Sql server 使用VARCHAR列透视SQL Server表时出现语法错误
我在SQL Server中有一个表(约50m行),如下所示:Sql server 使用VARCHAR列透视SQL Server表时出现语法错误,sql-server,pivot,pivot-table,Sql Server,Pivot,Pivot Table,我在SQL Server中有一个表(约50m行),如下所示: PId CustomColumnName CustomColumnValue 1 PropertyA 1 1 PropertyB B 1 PropertyC C 2 PropertyA 1.5 2 Prope
PId CustomColumnName CustomColumnValue
1 PropertyA 1
1 PropertyB B
1 PropertyC C
2 PropertyA 1.5
2 PropertyB BB
2 PropertyC CC
3 PropertyD D1
3 PropertyA 2.0
我想透视该表,使其看起来像这样:
PId PropertyA PropertyB PropertyC PropertyD
1 1 B C NULL
2 1.5 BB CC NULL
3 2.0 NULL NULL D1
SELECT *
FROM
(
SELECT [PId], [CustomColumnName], [CustomColumnValue]
FROM [myTable]
) AS src
PIVOT
(
MAX([CustomColumnValue]) -- Not sure how to aggregate VARCHAR data here
FOR [CustomColumnName] IN ('PropertyA', 'PropertyB', 'PropertyC', 'PropertyD')
) AS pvt
我知道SQL Server有PIVOT
功能,所以我写了如下内容:
PId PropertyA PropertyB PropertyC PropertyD
1 1 B C NULL
2 1.5 BB CC NULL
3 2.0 NULL NULL D1
SELECT *
FROM
(
SELECT [PId], [CustomColumnName], [CustomColumnValue]
FROM [myTable]
) AS src
PIVOT
(
MAX([CustomColumnValue]) -- Not sure how to aggregate VARCHAR data here
FOR [CustomColumnName] IN ('PropertyA', 'PropertyB', 'PropertyC', 'PropertyD')
) AS pvt
但是我在“PropertyA”附近得到了不正确的语法。我不确定使用SQL Server的PIVOT
功能是否可以完成我正在尝试的操作(因为我认为我无法聚合[CustomColumnValue]
,这是VARCHAR
类型)如果在我的用例中使用PIVOT
不可行,是否有一种有效的替代方法来实现我想要做的事情
提前感谢您的建议/回答 --删除单引号,您将获得所需的结果集
;With cte(PId, CustomColumnName,CustomColumnValue)
AS
(
SELECT 1,'PropertyA','1' Union all
SELECT 1,'PropertyB','B' Union all
SELECT 1,'PropertyC','C' Union all
SELECT 2,'PropertyA','1.5' Union all
SELECT 2,'PropertyB','BB' Union all
SELECT 2,'PropertyC','CC' Union all
SELECT 3,'PropertyD','D1' Union all
SELECT 3,'PropertyA','2.0'
)
SELECT *
FROM
(
SELECT [PId], [CustomColumnName], [CustomColumnValue]
FROM cte
) AS src
PIVOT
(
MAX([CustomColumnValue])
FOR [CustomColumnName] IN (PropertyA, PropertyB, PropertyC, PropertyD)
) AS pvt
您可以使用Dynamic Pivot生成相同的结果集
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
Drop table #temp
;With cte(PId, CustomColumnName,CustomColumnValue)
AS
(
SELECT 1,'PropertyA','1' Union all
SELECT 1,'PropertyB','B' Union all
SELECT 1,'PropertyC','C' Union all
SELECT 2,'PropertyA','1.5' Union all
SELECT 2,'PropertyB','BB' Union all
SELECT 2,'PropertyC','CC' Union all
SELECT 3,'PropertyD','D1' Union all
SELECT 3,'PropertyA','2.0'
)
SELECT * INTO #temp FROM cte
Declare
@Sql nvarchar(max),
@dynamicCol nvarchar(max)
--Create columns Dynamically
SELECT @dynamicCol=STUFF((SELECT DISTINCT ', '+ QUOTENAME(CustomColumnName )
From #temp For XML PATH ('')),1,1,'')
--SELECT @dynamicCol
SET @Sql='
SELECT [PId] ,'+ @dynamicCol +' From
(
SELECT [PId], [CustomColumnName], [CustomColumnValue] From
#temp
)AS Src
PIVOT
(
MAX([CustomColumnValue]) For [CustomColumnName] IN ('+@dynamicCol+')
)
AS Pvt'
PRINT @Sql
EXEC(@Sql)
--删除单引号,您将获得所需的结果集
;With cte(PId, CustomColumnName,CustomColumnValue)
AS
(
SELECT 1,'PropertyA','1' Union all
SELECT 1,'PropertyB','B' Union all
SELECT 1,'PropertyC','C' Union all
SELECT 2,'PropertyA','1.5' Union all
SELECT 2,'PropertyB','BB' Union all
SELECT 2,'PropertyC','CC' Union all
SELECT 3,'PropertyD','D1' Union all
SELECT 3,'PropertyA','2.0'
)
SELECT *
FROM
(
SELECT [PId], [CustomColumnName], [CustomColumnValue]
FROM cte
) AS src
PIVOT
(
MAX([CustomColumnValue])
FOR [CustomColumnName] IN (PropertyA, PropertyB, PropertyC, PropertyD)
) AS pvt
您可以使用Dynamic Pivot生成相同的结果集
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
Drop table #temp
;With cte(PId, CustomColumnName,CustomColumnValue)
AS
(
SELECT 1,'PropertyA','1' Union all
SELECT 1,'PropertyB','B' Union all
SELECT 1,'PropertyC','C' Union all
SELECT 2,'PropertyA','1.5' Union all
SELECT 2,'PropertyB','BB' Union all
SELECT 2,'PropertyC','CC' Union all
SELECT 3,'PropertyD','D1' Union all
SELECT 3,'PropertyA','2.0'
)
SELECT * INTO #temp FROM cte
Declare
@Sql nvarchar(max),
@dynamicCol nvarchar(max)
--Create columns Dynamically
SELECT @dynamicCol=STUFF((SELECT DISTINCT ', '+ QUOTENAME(CustomColumnName )
From #temp For XML PATH ('')),1,1,'')
--SELECT @dynamicCol
SET @Sql='
SELECT [PId] ,'+ @dynamicCol +' From
(
SELECT [PId], [CustomColumnName], [CustomColumnValue] From
#temp
)AS Src
PIVOT
(
MAX([CustomColumnValue]) For [CustomColumnName] IN ('+@dynamicCol+')
)
AS Pvt'
PRINT @Sql
EXEC(@Sql)
删除引号
PId | PropertyA | PropertyB | PropertyC | PropertyD
--: | :-------- | :-------- | :-------- | :--------
1 | 1 | B | C |空
2 | 1.5 | BB | CC |空
3 | 2.0 |零|零| D1
dbfiddle删除引号
PId | PropertyA | PropertyB | PropertyC | PropertyD
--: | :-------- | :-------- | :-------- | :--------
1 | 1 | B | C |空
2 | 1.5 | BB | CC |空
3 | 2.0 |零|零| D1
dbfiddle您需要使用列的quotename
SELECT *
FROM
(
SELECT [PId], [CustomColumnName], [CustomColumnValue]
FROM [myTable]
) AS src
PIVOT
(
MAX([CustomColumnValue]) -- Not sure how to aggregate VARCHAR data here
FOR [CustomColumnName] IN ([PropertyA], [PropertyB], [PropertyC], [PropertyD]) --provide in squarebrackets if generating dynamic then use quotename() function of sql server
) AS pvt
您需要使用列的quotename
SELECT *
FROM
(
SELECT [PId], [CustomColumnName], [CustomColumnValue]
FROM [myTable]
) AS src
PIVOT
(
MAX([CustomColumnValue]) -- Not sure how to aggregate VARCHAR data here
FOR [CustomColumnName] IN ([PropertyA], [PropertyB], [PropertyC], [PropertyD]) --provide in squarebrackets if generating dynamic then use quotename() function of sql server
) AS pvt
从列名PropertyA
周围取单引号,等等从列名PropertyA
周围取单引号,等等感谢您提供全面的答案,包括动态解决方案!感谢您提供全面的答案,包括动态解决方案!非常感谢。删除单引号并将其视为列名非常有效!我接受了另一个答案,因为s/he还提供了动态列的解决方案。希望您能理解。:)非常感谢。删除单引号并将其视为列名非常有效!我接受了另一个答案,因为s/he还提供了动态列的解决方案。希望您能理解。:)非常感谢。删除单引号并将其视为列名非常有效!我接受了另一个答案,因为s/he还提供了动态列的解决方案。希望您能理解。:)非常感谢。删除单引号并将其视为列名非常有效!我接受了另一个答案,因为s/he还提供了动态列的解决方案。希望您能理解。:)