2016-01-02 50 views
1

加入帶有過濾器的多個表我有3 tables..how我所說的日期參加這些表濾波器對每個表按日期

MainTbl

id accnt amount date 
--------------------------------- 
    1  xxx  10.00 1/1/2016 
    2  yyy  20.00 1/1/2016 
    3  zzz  30.00 1/1/2016 

SupplyTbl - 該表將增加的價值供給量在MainTbl

id accnt supply date 
------------------------------- 
    1  xxx  5.00  1/10/2016 
    1  xxx  5.00  2/14/2016 
    2  yyy 10.00  1/20/2016 

IssuedTbl

id accnt issued dateIssue 
------------------------------------------ 
    1  xxx 5.00  1/10/2016 
    1  xxx 5.00  2/14/2016 
    2  yyy 10.00  2/15/2016 

現在我想加入這些tables..with日期range..if我日期過濾器來JANUARY它會導致這樣的事情

id accnt amount issued 
----------------------------- 
    1  xxx 15.00 5.00 
    2  yyy 30.00 0.00 
    3  zzz 30.00 0.00 

,當我篩選出FEBRUARY ..它都將來自合併JANUARYFEBRUARY

id accnt amount issued 
----------------------------- 
    1  xxx 20.00 10.00 
    2  yyy 40.00 10.00 
    3  zzz 30.00 0.00 

我與此掙扎,因爲提前:)

+0

的你想要加入的性質尚未明確。 「SupplyTbl」與它有什麼關係? –

+0

你想要的過濾條件的性質還不清楚。你打算在哪個日期與過濾器進行比較?另外,你打算過濾掉從往年的日期,如果任何人出席? –

+0

的供應supplyTbl的值將在MainTbl..according增加量的ID..sir –

回答

1

其他night..glad任何help..tnx嘗試這樣的事情

SELECT M.id, 
     M.accnt, 
     amount = M.amount + Isnull(s.supply, 0), 
     issued = Isnull(I.issued, 0) 
FROM maintbl M 
     LEFT OUTER JOIN (SELECT id, 
           Sum(supply) AS supply 
         FROM supplytbl 
         WHERE Month(dates) = 1 -- Month filter for Jan - feb Month(dates) in (1,2) 
         GROUP BY id) S 
        ON s.id = M.id 
     LEFT OUTER JOIN (SELECT id, 
           Sum(issued) AS issued 
         FROM issuedtbl 
         WHERE Month(dateissue) = 1 -- Month filter 
         GROUP BY id) I 
        ON I.id = M.id 
+0

我們hmmm..how可以做到這一點查詢的意見,我會試試這個mate..tnx –

+0

?在過濾日期使用變量?像'WHERE月份(dateissue)= textbox.text'? –