Hello, I need to do a pivot in sql server 2014 but failed. I read several forums but none of them helped. Below is my Table that I would like to pivot:
Below is my desired result:
This is what I have so far but have an error:
- DECLARE @Query AS NVARCHAR(MAX)
- DECLARE @PivotProductType AS NVARCHAR(MAX)
-
-
- SELECT @PivotProductType= COALESCE(@PivotProductType + ',','') + QUOTENAME(ProductType)
- FROM (SELECT DISTINCT ProductType FROM tblAgentCommission) AS PivotColumn
-
- SELECT @PivotProductType
-
-
-
- SET @Query =
- N'SELECT Country, ' + @PivotProductType + '
- FROM [dbo].[PivotColumn]
- PIVOT(CommissionRate)
- FOR ProductType IN (' + @PivotProductType + ')) AS P'
-
- SELECT @Query
-
- EXEC sp_executesql @Query
(1 row(s) affected)
(1 row(s) affected)
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ')'.
Thanks in Advance.