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
對您重複詢問如何生成結果集是不合適的。從你的[上一個問題](http://stackoverflow.com/questions/3945837/how-to-join-two-tables)學習,甚至在一個小時前就問過。 – 2010-10-15 21:10:15
-1對於類似的問題在時間上如此接近。在IRC(聊天室)中,快速射擊Q和A可能更合適。 Google「IRC sql」。你可以在那裏進行對話,這可能會非常有幫助。 – orolo 2010-10-15 21:18:04
歡迎來到stackoverflow!有些人有時會有點胡思亂想,只是忽略他們並提出問題。 – 2010-10-15 21:32:42