How to create the Dynamic pivot table

Posted by Rajamani under Sql Server on 9/23/2012 | Points: 10 | Views : 2341 | Status : [Member] | Replies : 1
Hello friends, I wrote the pivot query but it didn't show the expected result. so i need how to Get the Group by in Customer_Name for my Pivot Query . This is my Query

DECLARE @columns nvarchar(max)

SELECT
@columns =
STUFF
(
(
SELECT DISTINCT
', [' + Product_Name + ']'
FROM
test
FOR XML PATH('')
), 1, 1, ''
)

EXEC
('
SELECT
*
FROM
(
SELECT
Customer_Name,
Product_Name,
Quantity,Revenue
FROM
test
) DATA
PIVOT
(
SUM(DATA.Quantity)
FOR
Product_Name
IN
(
' + @columns + '
)
) PVT
')





Responses

Login to post response