V
V
venom992021-10-24 14:39:09
SQL Server
venom99, 2021-10-24 14:39:09

How to PIVOT with the result for a group in one line?

The value for each month is calculated on a new line. Is there a way to concatenate strings?

SQL
WITH First AS (
SELECT
col_name [alias_1],
col_1 [alias_2],
datename(month, col_date) [month_alias_1],
month(col_date) [month_alias_2],
count(col_1) OVER (PARTITION BY MONTH(col_date), col_name ORDER BY col_name)*100/
count(col_1) OVER (PARTITION BY MONTH(col_date)) [procent]
FROM ...)
SELECT
*
FROM
First
PIVOT
(count(my_count)
FOR month_alias_1 IN ([Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec])
) as pivot_table
PIVOT
(sum(procent)
FOR month_alias_2 IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
) as pivot_table_1
GROUP BY alias_1,
[Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec],
[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

Result
6175455a29b82780186670.jpeg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question