日期:2014-05-18  浏览次数:20529 次

SQL 合并表的问题!高手请进
Table   1

OutDate     ItemNumber     Qty1
2007/7/1       A00001           20
      .
      .
      .
2007/7/31     A00001           30
(Table   1   中存放很多Itemnumber   在一个月内每一天的Qty1)

Table   2

OutDate         ItemNumber     Qty2
2007/7/5       A00001               21
2007/7/9       A00001               28
2007/7/14     A00901               14
...
(Table   2   中存放一些Itemnumber某月内的某些日期的Qty2)

Table   C

OutDate     Itemnumber   Qty1   Qty2
2007/7/1       A00001         21       0
.
.
.
2007/7/5       A00001         0         21
.
.
.
2007/7/31     A00001         31         0
2007/7/1       A00901         0           14
2007/7/31     A00901         0             0
......




------解决方案--------------------
try

Select
A.OutDate,
A.Itemnumber,
A.Qty1,
IsNull(B.Qty2, 0) As Qty2
From
Table1 A
Left Join
Table2 B
On A.OutDate = B.OutDate And A.Itemnumber = B.Itemnumber
------解决方案--------------------
--try:
--或者用full join
select A.OutDate , A.ItemNumber , A.Qty1, isnull(B.Qty2,0) as Qty2
from Table1 A
left join Table2 B
on A.OutDate=B.OutDate and A.ItemNumber=B.ItemNumber
------解决方案--------------------
(Table 1 中存放很多Itemnumber 在一个月内每一天的Qty1)

-------------
所以我覺得用Left Join應該就可以了