tags:

views:

62

answers:

2

Possible Duplicate:
How to join two tables

table 1

Date         StartingAum

07/01/2010     120
08/01/2010     220
09/01/2010     320

table 2

Date          DepContr   withdra    
01/01/2010      60          15
02/01/2010      70          25
03/01/2010      80          15
04/01/2010      30          89 
05/01/2010      40          15
06/01/2010      25          85
07/01/2010      16          17   
08/01/2010      19          21
09/01/2010      68          79

the output should be

Date         StartingAum     DepContr   withdra    
01/01/2010      0              60          15
02/01/2010      0              70          25
03/01/2010      0              80          15
04/01/2010      0              30          89 
05/01/2010      0              40          15
06/01/2010      0              25          85
07/01/2010     120             16          17   
08/01/2010     220             19          21
09/01/2010     320             68          79

i need the output exactly similar to that

+2  A: 
DECLARE @Table1 table ([date] datetime, StartingAum int)
DECLARE @Table2 table ([date] datetime, DepContr int, withdra int)
INSERT @Table1 VALUES ('07/01/2010',     120)
INSERT @Table1 VALUES ('08/01/2010',     220)
INSERT @Table1 VALUES ('09/01/2010',     320)

INSERT @Table2 VALUES ('01/01/2010',      60 ,         15)
INSERT @Table2 VALUES ('02/01/2010',      70 ,         25)
INSERT @Table2 VALUES ('03/01/2010',      80 ,         15)
INSERT @Table2 VALUES ('04/01/2010',      30 ,         89)
INSERT @Table2 VALUES ('05/01/2010',      40 ,         15)
INSERT @Table2 VALUES ('06/01/2010',      25 ,         85)
INSERT @Table2 VALUES ('07/01/2010',      16 ,         17)
INSERT @Table2 VALUES ('08/01/2010',      19 ,         21)
INSERT @Table2 VALUES ('09/01/2010',      68 ,         79)


SELECT 
    t2.[Date]
        ,ISNULL(t1.StartingAum, 0) AS StartingAum     
        ,t2.DepContr
        ,t2.withdra   
    FROM @Table2           t2
        LEFT JOIN @Table1  t1 ON t2.[Date] = t1.[Date]
    ORDER BY t2.[Date]

OUTPUT:

Date                    StartingAum DepContr    withdra
----------------------- ----------- ----------- -----------
2010-01-01 00:00:00.000 0           60          15
2010-02-01 00:00:00.000 0           70          25
2010-03-01 00:00:00.000 0           80          15
2010-04-01 00:00:00.000 0           30          89
2010-05-01 00:00:00.000 0           40          15
2010-06-01 00:00:00.000 0           25          85
2010-07-01 00:00:00.000 120         16          17
2010-08-01 00:00:00.000 220         19          21
2010-09-01 00:00:00.000 320         68          79
KM
the startingAum column is returning all 0's
rakesh
startingAum will return a zero if its Table1 value is a zero, or if the matching row is not in Table2. If there are any times on any of these date values (like `2010-01-01 13:45:23.123`) then they have to be exactly the same or the join will not find a match and startingAum will be zero. If the dates DO have any time on them, change the `LEFT JOIN` line to : `LEFT JOIN @Table1 t1 ON DATEADD(day,DATEDIFF(day,0,t2.[Date]),0) = DATEADD(day,DATEDIFF(day,0,t1.[Date]),0)` and it will join on just the date and not the time.
KM
if they are variable @temp tables or actual tempdb #temp tables shouldn't matter, just change `FROM @Table2 t2` to `FROM #Table2 t2` and `LEFT JOIN @Table1 t1` to `LEFT JOIN #Table1 t1` and you should be good to go.
KM
StartingAum coloumn is returning 0's
rakesh
A: 

I think you should have a look at this page: http://www.w3schools.com/sql/sql_union.asp That might be helpful?

BerggreenDK
sorry my answer might not be the best, but I need a better explanation of what you are trying to calculate before I can help you construct the SQL needed. I see some of the other answers are already doing that so I will wait and see if more answers needed or not. Sorry about the lame link.
BerggreenDK