I want the "May 1" of invoice amount column to be dynamic based on the bcd.processeddate
'5/1/2020 between '5/1/2021' A year worth of data so for example if I choose bcd.processeddate
5/15/2020 to 5/15/2021, the columns should say May 15 invoice amount
, june 15 invoice amount
and so on.
Can someone guide me in the right direction?
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,
bcd.MemberID, bcd.MemberStationCode, ca.name
order by 1, 2, bcd.MemberStationCode, bcd.MemberID