hello sir, my query are executed. but i had a problem my result it would show the Same customer_Name in many time. so how to group by the Customer_Name in my query
alter procedure sp_Example
as
begin
DECLARE @colsPivot AS NVARCHAR(MAX),
@colsUnpivot as NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @colsPivot = STUFF((SELECT distinct ',' + QUOTENAME(Product_Name)
from test
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query
= 'select *
from
(
select Customer_Name,Product_Name,Quantity, Revenue
from test
)X1
pivot
(
max(Quantity)
for Product_Name in ('+ @colspivot +')
) p'
exec(@query)
end