2017-03-05 28 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路径' –

相关问题