我已经从外部来源接收到了汇总格式的数据。我需要一种将其分解为适合我正在使用的系统的方法。
为了说明这一点,假设我收到的数据如下所示:
receivedTable: Age Gender Count 40 M 3 41 M 2
我希望这是一种分解格式,如下所示:
systemTable: ID Age Gender 1 40 M 2 40 M 3 40 M 4 41 M 5 41 M
感谢
根据计数范围的不同,您可以使用一个查找表,该查找表为每个整数x准确保存x个记录。像这样:
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
然后加入此表:
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