我想知道我留下了哪一部分。因为我得到了不同的方面结果。
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 如果count不等于0将显示2相同的结果名称,但计数值不同1名称将获得全0但另一个将具有计数值。
预计如下: https://gyazo.com/e163473da7dd16aa798f431e42588406
谢谢你们。
答案 0 :(得分: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