如何使用动态行对 Pivoted Table 进行分组?
How to Group on Pivoted Table with Dynamic Rows?
我有这样的数据集
[student][datetime][dictionary][value]
[1234 ][datetime][1 ][a ]
[1234 ][datetime][2 ][b ]
我想创建这样的数据集
[student][1 ][2 ]
[1234 ][a ][b ]
我知道 SQL 服务器枢轴是实现此目的的一种方式。我已经编写了以下代码来尝试对数据进行透视,但是它自然不会分组。
所以我的结果是这样的:
[student][datetime][1 ][2 ]
[1234 ][datetime][null ][b ]
[1234 ][datetime][a ][null ]
我怎样才能最好地将它们分组?我不关心在最终数据集中有其他源列,例如日期时间,只关心字典和值的矩阵
谢谢
DECLARE @columns AS VARCHAR(MAX);
DECLARE @sql AS VARCHAR(MAX);
SELECT @columns = substring((Select DISTINCT ',' + QUOTENAME(dictionary) FROM syuservalues FOR XML PATH ('')),2, 1000);
SELECT @sql =
'SELECT Pivoted.*
FROM syuservalues
PIVOT
( MAX(value)
FOR dictionary IN( ' + @columns + ' )) as Pivoted where student = 327392'; ' Will eventually want it for all students
EXECUTE(@sql);
对于这种问题,您可以使用任意聚合函数来去除空值。我通常使用 max
因为它可以用于数字和字符串数据类型。
select pivoted.student, [1] = max([1]), [2] = max([2])
from syuservalues
pivot(max(value) for dictionary in(...)) pivoted
group by pivoted.student
a
和 b
被带到两行,因为您已经为数据透视表添加了 [datetime]
列,为 a
和 [ 添加了 [datetime]
值=14=]会有所不同。
样本TABLE
CREATE TABLE #TEMP([student] INT,[datetime] DATETIME,[dictionary] INT,[value] VARCHAR(30))
INSERT INTO #TEMP
SELECT 1234,'2015-02-01',1,'a'
UNION ALL
SELECT 1234, '2015-02-01',2,'b'
声明一个变量以动态获取数据透视表的列
DECLARE @cols NVARCHAR (MAX)
SELECT @cols = COALESCE (@cols + ',[' + CAST([dictionary] AS VARCHAR(10)) + ']',
'[' + CAST([dictionary] AS VARCHAR(10)) + ']')
FROM
(
SELECT DISTINCT [dictionary]
FROM #TEMP
) PV
ORDER BY CAST([dictionary] AS INT)
现在转动它。我把逻辑写在里面了。
DECLARE @query NVARCHAR(MAX)
SET @query = '-- Your pivoted result is here
SELECT *
FROM
(
-- Select data before pivot. We are not taking [datetime] column because
-- it will not bring ur expected result
SELECT student,[dictionary],value
FROM #TEMP
) x
PIVOT
(
-- Value in each dynamic column
MIN(value)
-- Tell the columns to pivot
FOR [dictionary] IN (' + @cols + ')
) p
ORDER BY student;'
EXEC SP_EXECUTESQL @query
我有这样的数据集
[student][datetime][dictionary][value]
[1234 ][datetime][1 ][a ]
[1234 ][datetime][2 ][b ]
我想创建这样的数据集
[student][1 ][2 ]
[1234 ][a ][b ]
我知道 SQL 服务器枢轴是实现此目的的一种方式。我已经编写了以下代码来尝试对数据进行透视,但是它自然不会分组。
所以我的结果是这样的:
[student][datetime][1 ][2 ]
[1234 ][datetime][null ][b ]
[1234 ][datetime][a ][null ]
我怎样才能最好地将它们分组?我不关心在最终数据集中有其他源列,例如日期时间,只关心字典和值的矩阵
谢谢
DECLARE @columns AS VARCHAR(MAX);
DECLARE @sql AS VARCHAR(MAX);
SELECT @columns = substring((Select DISTINCT ',' + QUOTENAME(dictionary) FROM syuservalues FOR XML PATH ('')),2, 1000);
SELECT @sql =
'SELECT Pivoted.*
FROM syuservalues
PIVOT
( MAX(value)
FOR dictionary IN( ' + @columns + ' )) as Pivoted where student = 327392'; ' Will eventually want it for all students
EXECUTE(@sql);
对于这种问题,您可以使用任意聚合函数来去除空值。我通常使用 max
因为它可以用于数字和字符串数据类型。
select pivoted.student, [1] = max([1]), [2] = max([2])
from syuservalues
pivot(max(value) for dictionary in(...)) pivoted
group by pivoted.student
a
和 b
被带到两行,因为您已经为数据透视表添加了 [datetime]
列,为 a
和 [ 添加了 [datetime]
值=14=]会有所不同。
样本TABLE
CREATE TABLE #TEMP([student] INT,[datetime] DATETIME,[dictionary] INT,[value] VARCHAR(30))
INSERT INTO #TEMP
SELECT 1234,'2015-02-01',1,'a'
UNION ALL
SELECT 1234, '2015-02-01',2,'b'
声明一个变量以动态获取数据透视表的列
DECLARE @cols NVARCHAR (MAX)
SELECT @cols = COALESCE (@cols + ',[' + CAST([dictionary] AS VARCHAR(10)) + ']',
'[' + CAST([dictionary] AS VARCHAR(10)) + ']')
FROM
(
SELECT DISTINCT [dictionary]
FROM #TEMP
) PV
ORDER BY CAST([dictionary] AS INT)
现在转动它。我把逻辑写在里面了。
DECLARE @query NVARCHAR(MAX)
SET @query = '-- Your pivoted result is here
SELECT *
FROM
(
-- Select data before pivot. We are not taking [datetime] column because
-- it will not bring ur expected result
SELECT student,[dictionary],value
FROM #TEMP
) x
PIVOT
(
-- Value in each dynamic column
MIN(value)
-- Tell the columns to pivot
FOR [dictionary] IN (' + @cols + ')
) p
ORDER BY student;'
EXEC SP_EXECUTESQL @query