Disaggregate a generalized table in SQL Server 2008
I got data from an external source, which is in a generic format. I need a way to disaggregate this to fit in with the system I am using.
To illustrate, let's assume the data I received looks like this:
receivedTable:
Age Gender Count
40 M 3
41 M 2
I want this disaggregated format:
systemTable:
ID Age Gender
1 40 M
2 40 M
3 40 M
4 41 M
5 41 M
Thanks
Karl
+2
source to share
2 answers
Depending on the range of your count, you can use a lookup table containing exactly x records for each integer x. Like this:
create table counter(num int)
insert into counter select 1
insert into counter select 2
insert into counter select 2
insert into counter select 3
insert into counter select 3
insert into counter select 3
insert into counter select 4
insert into counter select 4
insert into counter select 4
insert into counter select 4
then join this table:
create table source(age int, gender char(1), num int)
insert into source select 40, 'm', 3
insert into source select 30, 'f', 2
insert into source select 20, 'm', 1
--insert into destination(age, gender)
select age, gender
from source
inner join counter on counter.num = source.num
+1
source to share
From "Works on my machine (TM)" a stable recursive query with all the usual caveats about the maximum recursion depth.
with Expanded(exAge, exGender, exRowIndex) as
(
select
Age as exAge,
Gender as exGender,
1 as exRowIndex
from
tblTest1
union all
select
exAge,
exGender,
exRowIndex+1
from
tblTest1 t1
inner join
Expanded e on (e.exAge = t1.Age and e.exGender = t1.Gender and e.exRowIndex < t1.Count)
)
select
exAge,
exGender,
exRowIndex
from
Expanded
order by
exAge,
exGender,
exRowIndex
option (MAXRECURSION 0) -- BE CAREFUL!!
You won't get the row id, but inserting the query result into a table with an identity column will deal with that.
+2
source to share