我有兩個表,即 「CProduct」 和 「DPRODUCT」。下面是例子:使用LEFT JOIN,聯盟和右加入以獲得期望的結果
CProduct:
EffectiveDate CFund
2014-01-03 0.06
2014-01-03 0.12
2014-01-06 0.11
DPRODUCT:
EffectiveDate DFund
2014-01-03 0.06
2014-01-06 0.12
2014-01-08 0.09
我想要得到的結果如下圖所示:
EffectiveDate CFund DFund
2014-01-03 0.18 0.06
2014-01-06 0.11 0.12
2014-01-08 NULL 0.09
我的查詢是:
SELECT a.EffectiveDate,a.CFund,a.DFund
FROM (
SELECT t1.EffectiveDate,Sum(t1.CFund) as CFund ,SUM(t2.DFund) as DFund FROM CProduct t1
LEFT JOIN DProduct t2 ON t1.EffectiveDate = t2.EffectiveDate Group By t1.EffectiveDate
UNION
SELECT t1.EffectiveDate,SUM(t2.CFund) as CFund ,Sum(t1.DFund) as DFund FROM DProduct t1
LEFT JOIN CProduct t2 ON t1.EffectiveDate = t2.EffectiveDate Group By t1.EffectiveDate
) a
但我沒有得到預期的結果。
有兩個問題:1. DProduct中是否有重複的日期? 2. DProduct中是否存在CProduct中不存在的日期? – 2015-01-21 08:34:36
@ZThrosten是這兩種情況都可能存在。 – vivek 2015-01-21 08:54:42