views:

43

answers:

2

Hi

I've received data from an external source, which is in a summarised format. I need a way to disaggregate this to fit into a system I am using.

To illustrate, suppose the data I received looks like this:

receivedTable:

Age     Gender     Count
40      M          3
41      M          2

I want this is a disaggregated format like this:

systemTable:

ID      Age        Gender
1       40         M          
2       40         M 
3       40         M 
4       41         M          
5       41         M 

Thanks
Karl

+1  A: 

Depending of the range of your count you could use a lookup table that holds 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 with 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
edosoft
Cool, integers table. This would work perfectly. My count range goes up into the thousands, so I guess I'll need a script to generate the table automatically
Karl
+1  A: 

From the "Works on my machine (TM)" stable a recursive query, with all the usual caveats about 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 don't get the row identifier - but inserting the result of the query into a table with an identity column would deal with that.

Neil Moss