2017-03-05 250 views
0

我想知道我離開它的哪一部分。因爲我得到不同方面的結果。(存儲過程)如何在12個月內查詢年度報告顯示

select @fYear as [Year], 
     main.Description, 
     --CardType, 
     (case when COUNT(jan.CardType) is null then '0' else COUNT(jan.CardType) end) as Jan_Collection, 
     (case when COUNT(feb.CardType) is null then '0' else COUNT(feb.CardType) end) as Feb_Collection, 
     (case when COUNT(mac.CardType) is null then '0' else COUNT(mac.CardType) end) as mac_Collection, 
     (case when COUNT(apr.CardType) is null then '0' else COUNT(apr.CardType) end) as apr_Collection, 
     (case when COUNT(may.CardType) is null then '0' else COUNT(may.CardType) end) as may_Collection, 
     (case when COUNT(jun.CardType) is null then '0' else COUNT(jun.CardType) end) as jun_Collection, 
     (case when COUNT(jul.CardType) is null then '0' else COUNT(jul.CardType) end) as jul_Collection, 
     (case when COUNT(aug.CardType) is null then '0' else COUNT(aug.CardType) end) as aug_Collection, 
     (case when COUNT(sep.CardType) is null then '0' else COUNT(sep.CardType) end) as sep_Collection, 
     (case when COUNT(oct.CardType) is null then '0' else COUNT(oct.CardType) end) as oct_Collection, 
     (case when COUNT(nov.CardType) is null then '0' else COUNT(nov.CardType) end) as nov_Collection, 
     (case when COUNT(dis.CardType) is null then '0' else COUNT(dis.CardType) end) as dis_Collection 
     from BKM_Requestor as main 
       left join BKM_Party as b on main.requestorid = b.requestorid 
       left join tblBlue as jan on b.partyid = jan.partyid and MONTH(jan.AppliedDate) = '1' and YEAR(jan.AppliedDate) = @fYear and jan.CardType = 'K' and b.PartyId = jan.PartyId 
       left join tblBlue as feb on b.partyid = feb.partyid and MONTH(feb.AppliedDate) = '2' and YEAR(feb.AppliedDate) = @fYear and feb.CardType = 'K' and b.PartyId = feb.PartyId 
       left join tblBlue as mac on b.partyid = mac.partyid and MONTH(mac.AppliedDate) = '3' and YEAR(mac.AppliedDate) = @fYear and mac.CardType = 'K' and b.PartyId = mac.PartyId 
       left join tblBlue as apr on b.partyid = apr.partyid and MONTH(apr.AppliedDate) = '4' and YEAR(apr.AppliedDate) = @fYear and apr.CardType = 'K' and b.PartyId = apr.PartyId 
       left join tblBlue as may on b.partyid = may.partyid and MONTH(may.AppliedDate) = '5' and YEAR(may.AppliedDate) = @fYear and may.CardType = 'K' and b.PartyId = may.PartyId 
       left join tblBlue as jun on b.partyid = jun.partyid and MONTH(jun.AppliedDate) = '6' and YEAR(jun.AppliedDate) = @fYear and jun.CardType = 'K' and b.PartyId = jun.PartyId 
       left join tblBlue as jul on b.partyid = jul.partyid and MONTH(jul.AppliedDate) = '7' and YEAR(jul.AppliedDate) = @fYear and jul.CardType = 'K' and b.PartyId = jul.PartyId 
       left join tblBlue as aug on b.partyid = aug.partyid and MONTH(aug.AppliedDate) = '8' and YEAR(aug.AppliedDate) = @fYear and aug.CardType = 'K' and b.PartyId = aug.PartyId  
       left join tblBlue as sep on b.partyid = sep.partyid and MONTH(sep.AppliedDate) = '9' and YEAR(sep.AppliedDate) = @fYear and sep.CardType = 'K' and b.PartyId = sep.PartyId 
       left join tblBlue as oct on b.partyid = oct.partyid and MONTH(oct.AppliedDate) = '10' and YEAR(oct.AppliedDate) = @fYear and oct.CardType = 'K' and b.PartyId = oct.PartyId 
       left join tblBlue as nov on b.partyid = nov.partyid and MONTH(nov.AppliedDate) = '11' and YEAR(nov.AppliedDate) = @fYear and nov.CardType = 'K' and b.PartyId = nov.PartyId 
       left join tblBlue as dis on b.partyid = dis.partyid and MONTH(dis.AppliedDate) = '12' and YEAR(dis.AppliedDate) = @fYear and dis.CardType = 'K' and b.PartyId = dis.PartyId 

      group by Description,jan.CardType,feb.CardType,mac.CardType,apr.CardType,may.CardType,jun.CardType,jul.CardType,aug.CardType,sep.CardType,oct.CardType,nov.CardType,dis.CardType 

END

現在輸出是這樣基於此查詢: https://gyazo.com/29fa98c207e81578cae95dcbaa97e0b9 如果算上不等於0將顯示2相同的結果名稱,但計數值不同的1名將獲得所有0,但另一個將有計數值。

aspected是這樣的: https://gyazo.com/e163473da7dd16aa798f431e42588406

謝謝你們。

回答

1

你並不真的需要加入同一個表的12倍來算的記錄,這樣的事情應該工作:

select 
    @fYear as [Year], 
    main.Description, 
    sum(case when MONTH(blue.AppliedDate) = 1 then 1 else 0 end)) as Jan_Collection, 
    sum(case when MONTH(blue.AppliedDate) = 2 then 1 else 0 end)) as Feb_Collection, 
    ... 
from BKM_Requestor as main 
    left join BKM_Party as b on main.requestorid = b.requestorid 
    left join tblBlue as blue on b.partyid = blue.partyid and YEAR(blue.AppliedDate) = @fYear and blue.CardType = 'K' 
group by main.Description 
+0

哦ICIC ..真棒。感謝兄弟 – ikramlim

+0

如果我想結合2名稱和記錄到1記錄是可以嗎? – ikramlim

+0

對不起,描述真的很難弄清楚你的意思。如果你的意思是像逗號分隔的列表,看看'for xml路徑' –

相關問題