This query list each contributor by year. What I would like is to see
Contributor Year 1 Year 2 Year 3 Year 4
For example:
Contributor Year 1 Year 2 Year 3 Year 4
100 $100 $200 $300 $400
200 $50 $100 $150 $200
SELECT year(t2.date) as YearContrib,t2.MemberNumberId,sum(coalesce([Amount],0)) as Amount
FROM [Contribution Details] t1
inner join [Contribution Headers] t2 on t1.ContribHeaderId=t2.ContribNumberId
group by t2.MemberNumberId,year(t2.date)
order by MemberNumberId,year(t2.date)
How do I restructure this query to accomplish this? I am using SQL Server 2012
Open in new window
incase if the number of years are dynamic and you don't know what they will be then you have to use a dynamic PIVOT as we do below
Open in new window