Лично я бы решил это в базе данных.Создайте таблицу в своей БД для хранения необработанных данных из таблицы Excel.Затем вы можете решить эту проблему в SQL следующим образом:
create table #excel (
range nvarchar(7),
location nvarchar(20),
col_0 nvarchar(1),
col_1 nvarchar(1),
col_2 nvarchar(1),
col_3 nvarchar(1),
col_4 nvarchar(1),
col_5 nvarchar(1),
col_6 nvarchar(1),
col_7 nvarchar(1),
col_8 nvarchar(1),
col_9 nvarchar(1)
)
/*Use SSIS to load your Excel sheet in, instead of this insert*/
insert into #excel
values ('0113221', 'Leeds', 'Y', NULL, 'Y', 'Y', NULL, 'Y', 'Y', NULL, 'Y', NULL)
;with numbers as
(
select 0 x
union all
select x + 1
from numbers
where x < 99
)
select e.location, e.range + '0' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_0 = 'Y'
union all
select e.location, e.range + '1' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_1 = 'Y'
union all
select e.location, e.range + '2' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_2 = 'Y'
union all
select e.location, e.range + '3' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_3 = 'Y'
union all
select e.location, e.range + '4' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_4 = 'Y'
union all
select e.location, e.range + '5' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_5 = 'Y'
union all
select e.location, e.range + '6' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_6 = 'Y'
union all
select e.location, e.range + '7' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_7 = 'Y'
union all
select e.location, e.range + '8' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_8 = 'Y'
union all
select e.location, e.range + '9' + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.col_9 = 'Y'
Если вы можете нормализовать ваши данные при загрузке в следующий формат (или аналогичный), вы можете сэкономить много неопрятного кода:
create table #excel (
range nvarchar(7),
location nvarchar(20),
number nvarchar(1),
yes_no nvarchar(1)
)
insert into #excel
values ('0113221', 'Leeds', '0', 'Y'),
('0113221', 'Leeds', '1', NULL),
('0113221', 'Leeds', '2', 'Y'),
('0113221', 'Leeds', '3', 'Y'),
('0113221', 'Leeds', '4', NULL),
('0113221', 'Leeds', '5', 'Y'),
('0113221', 'Leeds', '6', 'Y'),
('0113221', 'Leeds', '7', NULL),
('0113221', 'Leeds', '8', 'Y'),
('0113221', 'Leeds', '9', NULL)
;with numbers as
(
select 0 x
union all
select x + 1
from numbers
where x < 99
)
select e.location, e.range + e.number + RIGHT('00' + CAST(n.x as nvarchar), 3)
from #excel e cross join
numbers n
where e.yes_no = 'Y'
Мой SSIS немного ржавый, и передо мной нет экземпляра, с которым можно было бы поиграть, поэтому я боюсь, что не смогу помочь вам с частью нормализации.