sql server - Combine tables into one table -
i have 2 tables, each 1 has 'date','time' , 'id' columns , 100 columns that's represents counters, have master table contains 'date','time' , 'id' columns plus has counters exited in other 2 tables.
i need way update master tables 4 tables, below:
table 1 date,time,id,counter_a,counter_b 01012014,00:00:00,1,10,20 01012014,00:00:00,2,7,8 21012014,00:00:00,1,3,1 table 2 date,time,id,counter_c,counter_d 01012014,00:00:00,1,30,40 01012014,00:00:00,2,5,9 21012014,00:00:00,1,4,2` master table date,time,id,counter_a,counter_b,counter_c,counter_d 01012014,00:00:00,1,10,20,30,40 01012014,00:00:00,2,7,8,5,9 21012014,00:00:00,1,3,1,4,2
i tried dynamic sql insert it's takes long have 100,000 row in each of table 1 , table 2 , times it's timeout expiry error.
by way daily updates (tables 1 , table 2) csv files, there way update master table out taking time.
more detail:
hi, thank answer,
let me explain more: i'm trying storing statistics system, statistics counters operations , operations success , fail rates , etc...
i hourly statistics every day, them in 4 csv files, processed gathering of these data tables, 4 new tables every day.
so each id 24 value each counters, have 300 counters files.
the master table has 300 columns, times when export csv files system, neglect counters or add counters (columns).
the data in system in way: date time id cola colb .......... colc cold
but export system has limitations in number of exported columns (counters) per file, have separate columns 4 files, date, time , id same counters differs file file.
my approach combine counters in 1 table in system.
you use union all
statement, allows combine data multiple tables single dataset.
if have columns missing (counter_c,counter_d in table1) substitute them null
or default values (0, '',...).
if want unique values (if same value 2 tables should not inserted) instead of using union all
should use union
.
insert mastertable (date,time,id,counter_a,counter_b,counter_c,counter_d) select date,time,id,counter_a,counter_b,null,null table1 union select date,time,id,null,null,counter_c,counter_d table2 union ...
if want update values have list columns , wrap unionised select statement.
update m set date = x.date, time = x.time, id = x.id, counter_a = x.counter_a, counter_b = x.counter_b, counter_c = x.counter_c, counter_d = x.counter_d) mastertable m, ( select date,time,id,counter_a,counter_b,null,null table1 union select date,time,id,null,null,counter_c,counter_d table2 union ...) x m.id = x.id
Comments
Post a Comment