| Author |
Topic |
|
voyager838
Yak Posting Veteran
90 Posts |
Posted - 2009-03-17 : 04:07:24
|
| HiI want to combine a several tables to a new tableSo its look like below. The datetime field must be sync togetherand the new table should have all these columns in it.Table 1DateTime | col 1 | col 2 | col 32007.01.01 00:00 | 4.4 | 9 | 52007.01.02 00:00 | 2.5 | 11 | 7.2Table 2DatumTid | col 4| col 5 | col 62007.01.01 00:00 | 1.3 | 9 | 52007.01.02 00:00 | 7.5 | 4 | 9Table 3DateTime | Col 7 | Col 8 2007.01.01 00:00 | 0 |4 2007.01.02 00:00 | null | null2007.01.03 00:00 | 3 | 1Where 'col n' is the columnname, n are the number.New Table shold than look like thisDateTime | Col 1 |Col 2 |Col 3 |Col 4 |Col 5 |Col 6 |Col 7 |Col 82007.01.01 00:00 | 4.4 | 9 | 5 | 1.3 | 9 | 5| 0 |4 2007.01.02 00:00 | 2.5 | 11 | 7.2| 7.5 | 4 | 9 | null | null2007.01.03 00:00 |null |null|null|null|null|null| 3 | 1 Thanks |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2009-03-17 : 04:18:34
|
A full join between these three tables should suffice.Something like this:declare @t1 table( dt datetime, col1 float , col2 float, col3 float)declare @t2 table( dt datetime, col4 float , col5 float, col6 float)declare @t3 table( dt datetime, col7 float , col8 float)insert @t1select '20070101', 4.4, 9, 5 union allselect '2007.01.02', 2.5, 11 , 7.2insert @t2select '20070101', 1.3 , 9 , 5 union allselect '20070102', 7.5, 4 , 9insert @t3select '20070101', 0,4 union allselect '20070102', null, null union allselect '20070103', 3, 1select coalesce(t1.dt,t2.dt,t3.dt) as dt, col1, col2, col3, col4, col5, col6, col7, col8from @t1 t1 full join @t2 t2 on t1.dt = t2.dtfull join @t3 t3 on t2.dt = t3.dt Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
voyager838
Yak Posting Veteran
90 Posts |
Posted - 2009-03-17 : 04:52:29
|
| Thanks!Maby i got another idea also.Beacuse i dont want to be dependent of the server that are using.I really dont want to use T-SQL, even if its elegant.Now, this should also workdeclare @time table( dt datetime)declare @t1 table( dt datetime, col1 float , col2 float, col3 float)declare @t2 table( dt datetime, col4 float , col5 float, col6 float)declare @t3 table( dt datetime, col7 float , col8 float)insert @timeselect '20070101'union allselect '20070102' union allselect '20070103'insert @t1select '20070101', 4.4, 9, 5 union allselect '20070102', 2.5, 11 , 7.2insert @t2select '20070101', 1.3 , 9 , 5 union allselect '20070102', 7.5, 4 , 9insert @t3select '20070101', 0,4 union allselect '20070102', null, null union allselect '20070103', 3, 1SELECT time1.dt,t1.Col1, t1.Col2, t1.Col3, t2.Col4, t2.Col5, t2.Col6,t3.Col7, t3.Col8FROM @time time1LEFT OUTER JOIN @t1 t1 ON time1.dt = t1.dt LEFT OUTER JOIN @t2 t2 ON time1.dt = t2.dt LEFT OUTER JOIN @t3 t3 ON time1.dt = t3.dtThe resultdt Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col82007-01-01 00:00:00.000 4,4 9 5 1,3 9 5 0 42007-01-02 00:00:00.000 2,5 11 7,2 7,5 4 9 NULL NULL2007-01-03 00:00:00.000 NULL NULL NULL NULL NULL NULL 3 1With these you also can control with datetime tablewhen/which dates we want to collect.Easy to maintain in some point of view.”Relationship is surely the mirror in which you discover yourself.” (KRISHNAMURTI) |
 |
|
|
|
|
|