Skip to content
Advertisement

Query and Pivot Column for the last last 3 Years SQL

My need is to retrieve sell data from and pivot the last three years and sum the quantities. See example below. I wonder how to do it. I read that SQL pivot is the way to go, but I can’t figure out how to deal with pivoting “automatically” N number of years in the past.

INPUT

OUTPUT

Advertisement

Answer

You can try PIVOT opertor in SQL Server. I am applying YEAR function to get current year and subtracting 3 years from it to filter the dates.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement