SQLTeam.com | Weblogs | Forums

Using pivot


#1

I have used following sql query to extract customerwise, Financial Year wise Sales.

SELECT [Source No_], YEAR(DATEADD(Month, - 3, [Posting Date])) AS FY, SUM([Sales Amount (Actual)]) AS Sales
FROM [Pneumax India Pvt Ltd$Item Analysis View Entry]
WHERE ([Entry Type] = 0) AND ([Item Ledger Entry Type] = 1) AND ([Analysis Area] = 0) AND ([Analysis View Code] = N'prodgr') AND ([Source No_] = N'C00035')
GROUP BY YEAR(DATEADD(Month, - 3, [Posting Date])), [Source No_]
HAVING (YEAR(DATEADD(Month, - 3, [Posting Date])) = 2015) OR
(YEAR(DATEADD(Month, - 3, [Posting Date])) = 2016) OR
(YEAR(DATEADD(Month, - 3, [Posting Date])) = 2017)

I get output in following format using above query:-

Source No_ FY Sales
C00035 2015 311
C00035 2016 105
C00035 2017 162

But I need output in following format:-

Source No_ 2015 2016 2017
C00035 311 105 162

How to achieve the result in above format using Pivot? Please help.


#2

Post create table script and insert statements with sample data.