Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
zoe2003
Starting Member
17 Posts |
Posted - 2012-03-01 : 15:14:01
|
| Hi all,I have 2 tables:this one:user_id date sum1 01/01/2000 201 01/02/2000 301 01/03/2000 402 01/04/2000 502 01/05/2000 602 01/06/2000 70and this one:date01/01/200001/02/200001/03/200001/04/200001/05/200001/06/200001/07/200001/08/200001/09/200001/10/200001/11/200001/12/20001/13/2000I want to merge them inyo one table as following :user_id date sum1 01/01/2000 201 01/02/2000 301 01/03/2000 401 01/04/2000 01 01/05/2000 01 01/06/2000 01 01/07/2000 01 01/08/2000 01 01/09/2000 01 01/10/2000 01 01/11/2000 01 01/12/2000 01 1/13/2000 02 01/01/2000 02 01/02/2000 02 01/03/2000 02 01/04/2000 502 01/05/2000 602 01/06/2000 702 01/07/2000 02 01/08/2000 02 01/09/2000 02 01/10/2000 02 01/11/2000 02 01/12/2000 02 1/13/2000 0How can I do it using sql query ?Thanks in advance. |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2012-03-01 : 16:02:00
|
| [code]SELECT t1.user_id, t2.date, isnull(t1.sum, 0) [sum]FROM table2 t2LEFT JOIN table1 t1ON t1.date = t2.date;[/code] |
 |
|
|
zoe2003
Starting Member
17 Posts |
Posted - 2012-03-01 : 16:25:35
|
| with your query i get this results:user_id date sum1 01/01/2000 201 01/02/2000 301 01/03/2000 40null 01/04/2000 0null 01/05/2000 0null 01/06/2000 0null 01/07/2000 0null 01/08/2000 0null 01/09/2000 0null 01/10/2000 0null 01/11/2000 0null 01/12/2000 0null 1/13/2000 0null 01/01/2000 0null 01/02/2000 0null 01/03/2000 02 01/04/2000 502 01/05/2000 602 01/06/2000 70null 01/07/2000 0null 01/08/2000 0null 01/09/2000 0null 01/10/2000 0null 01/11/2000 0null 01/12/2000 0null 1/13/2000 0I need that the user_id will appear in every row. like the example I gave. any ideas ? |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2012-03-01 : 16:41:00
|
| [code]SELECT x.user_id, x.date, isnull(t1.sum, 0) [sum]FROM ( SELECT distinct t1.user_id, t2.date FROM #table2 t2 cross apply #table1 t1)xLEFT JOIN #table1 t1On t1.user_id = x.user_idAnd t1.date = x.date;[/code] |
 |
|
|
zoe2003
Starting Member
17 Posts |
Posted - 2012-03-02 : 04:58:08
|
| Thank you Russel, it's good !!! |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2012-03-02 : 07:16:32
|
You're Welcome. |
 |
|
|
|
|
|