微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我希望发票金额列的“5 月 1 日”基于 bcd.processeddate '5/1/2020 between '5/1/2021' 一年的数据

如何解决我希望发票金额列的“5 月 1 日”基于 bcd.processeddate '5/1/2020 between '5/1/2021' 一年的数据

我希望发票金额列的“May 1”是动态的,基于 bcd.processeddate '5/1/2020 between '5/1/2021' 一年的数据,例如,如果我选择bcd.processeddate 5/15/2020 至 5/15/2021,各列应显示 May 15 invoice amountjune 15 invoice amount 等。

有人可以指导我朝着正确的方向前进吗?

select 
       case when cagp.amb_memberid is not null then cagp.amb_memberid else case when cap.amb_memberid is not null then cap.amb_memberid else ca.amb_memberid end end as [ParentID],case when cagp.amb_memberid is not null then cagp.name else case when cap.amb_memberid is not null then cap.name else ca.name end end as [ParentName],bcd.MemberID,bcd.MemberStationCode,ca.name,sum(case when year(processeddate)=2020 and month(processeddate)=5 then (invoiceAmount) else 0 end) as "May 1 invoiceAmount",sum(case when year(processeddate)=2020 and month(processeddate)=6 then (invoiceAmount) else 0 end) as "Jun 1 invoiceAmount",sum(case when year(processeddate)=2020 and month(processeddate)=7 then (invoiceAmount) else 0 end) as "Jul 1 invoiceAmount",sum(case when year(processeddate)=2020 and month(processeddate)=8 then (invoiceAmount) else 0 end) as "Aug 1 invoiceAmount",sum(case when year(processeddate)=2020 and month(processeddate)=9 then (invoiceAmount) else 0 end) as "Sep 1 invoiceAmount",sum(case when year(processeddate)=2020 and month(processeddate)=10 then (invoiceAmount) else 0 end) as "Oct 1 invoiceAmount",sum(case when year(processeddate)=2020 and month(processeddate)=11 then (invoiceAmount) else 0 end) as "Nov 1 invoiceAmount",sum(case when year(processeddate)=2020 and month(processeddate)=12 then (invoiceAmount) else 0 end) as "Dec 1 invoiceAmount",sum(case when year(processeddate)=2021 and month(processeddate)=1 then (invoiceAmount) else 0 end) as "Jan 1 invoiceAmount",sum(case when year(processeddate)=2021 and month(processeddate)=2 then (invoiceAmount) else 0 end) as "Feb 1 invoiceAmount",sum(case when year(processeddate)=2021 and month(processeddate)=3 then (invoiceAmount) else 0 end) as "Mar 1 invoiceAmount",sum(case when year(processeddate)=2021 and month(processeddate)=4 then (invoiceAmount) else 0 end) as "Apr 1 invoiceAmount",sum(case when year(processeddate)=2021 and month(processeddate)=5 then (invoiceAmount) else 0 end) as "May 1 invoiceAmount"
from serviceconnection_Hist bcd 
left outer join CRM_Account ca
       on bcd.MemberID=ca.amb_memberid and ca.statecodename='Active' and ca.statecode=0 and ca.statuscode=1
left outer join CRM_Account cap on ca.parentaccountid=cap.accountid and cap.statecode=0 and cap.statuscode=1
left outer join CRM_Account cagp on cap.parentaccountid=cagp.accountid and cagp.statecode=0 and cagp.statuscode=1
where bcd.processeddate >= '5/1/2020' and bcd.processeddate< '5/1/2021' and MemberID<>'' and ca.statecode=0 and ca.statuscode=1
group by 
       case when cagp.amb_memberid is not null then cagp.amb_memberid else case when cap.amb_memberid is not null then cap.amb_memberid else ca.amb_memberid end end,case when cagp.amb_memberid is not null then cagp.name else case when cap.amb_memberid is not null then cap.name else ca.name end end,ca.name
order by 1,2,bcd.MemberID

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。