输入
+---------+--------------------+-----------+
| id | reason | location |
+---------+--------------------+-----------+
| 1077501 | credit_card | AZ |
| 1077430 | car | GA |
| 1077175 | small_business | IL |
| 1076863 | other | CA |
| 1075358 | other | OR |
| 1075269 | wedding | AZ |
| 1069639 | debt_consolidation | NC |
| 1072053 | car | CA |
| 1071795 | small_busines | CA |
+---------+--------------------+-----------+
输出应该是
+----------+-------------+-----+----------------+----------+---------+-------+
| location | credit_card | car | small_business | other... | wedding | total |
+----------+-------------+-----+----------------+----------+---------+-------+
| AZ | 1 | 0 | 0 | 0 | 1 | 2 |
| CA | 0 | 1 | 1 | 0 | 0 | 2 |
+----------+-------------+-----+----------------+----------+---------+-------+
输出应该是上述格式的原因,每个位置都是一列。
答案 0 :(得分:0)
将Pivot与动态列一起使用
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
--Get distinct values of the PIVOT Column
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME(reason)
FROM (SELECT DISTINCT reason FROM #T) AS Courses
--Prepare the PIVOT query using the dynamic
SET @DynamicPivotQuery =
N'SELECT location, ' + @ColumnName +',Total = ' + REPLACE(@ColumnName,',[', ' + [') +'
FROM #T
PIVOT(count(Id)
FOR reason IN (' + @ColumnName + ')) AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery