I have been researching and would like to use SQL's built in ROLLUP command on my data, but that has proven not to work for me. Any help would be greatly appreciated.
Here is the SQL pivot query I have created using Dynamic Columns. I want to add a Total column at the end that sums up the count.
DECLARE @SQL as VARCHAR(MAX)
DECLARE @Columns AS VARCHAR(MAX)
SELECT @Columns =
COALESCE(@Columns + ', ','') + QUOTENAME(order_date)
FROM
(
SELECT DISTINCT order_date, order_month
FROM rpt_sales_performance
) AS B
ORDER BY B.order_month
SET @SQL = '
WITH PivotData AS
(
SELECT
order_date,
sales_person
FROM rpt_sales_performance
)
SELECT
sales_person,
' + @Columns + '
FROM PivotData
PIVOT
(
COUNT(order_date)
FOR order_date
IN (' + @Columns + ')
) AS PivotResult
ORDER BY sales_person'
EXEC (@SQL)
Aucun commentaire:
Enregistrer un commentaire