可能重复:
How to join two tables
表1
Date StartingAum
07/01/2010 120
08/01/2010 220
09/01/2010 320
表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
输出应为
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
我需要与
完全相似的输出答案 0 :(得分:2)
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]
输出:
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
答案 1 :(得分:0)
我想你应该看看这个页面:http://www.w3schools.com/sql/sql_union.asp 那可能有帮助吗?