Pivoting in MS SQL Server


SELECT
*
FROM
(
SELECT
p.product_name,
g.tx_id,
g.transmonth
FROM
[DataAnalytics].[dbo].[tbla_hst_gross_add_policy] g
INNER JOIN
[DataAnalytics].[dbo].[tbld_product] p
ON
g.product_code = p.product_code and transmonth>='202101'
) t
PIVOT
(
COUNT(polnum)
FOR product_name
IN ([Product 1],[Product 2],[Product 3],[Product 4],[Product 5],[Product 6],[Product 7],[Product 8],[Product 9])
) AS pvt;

Tags:,

Add a Comment