为什么Pivot会在“4721”附近给出“错误语法”

时间:2022-09-03 23:14:36

I have a table like below -

我有一张下面这样的桌子

[ID]    [PID]
1       4721
2       25
3       4721

And I create a Pivot using the below mentioned Query -

我使用下面提到的查询创建了一个轴心

select id,pid
from #a
pivot(sum(id) for pid in(4721,25)) as pvt

but I am getting error in the above code as - Incorrect syntax near '4721'. I am not able to figure out why. Can you tell me where I am missing. I want the resultant table like -

但是我在上面的代码中有错误,因为在'4721'附近语法不正确。我不知道为什么。你能告诉我我在哪里吗?我想要合成的表格

[4721]  [25]
4,       2

1 个解决方案

#1


4  

I think using [,] can solve it

我认为使用[,]可以解决这个问题

select id,pid
from #a
pivot(sum(id) for pid in([4721],[25])) as pvt

The syntax for PIVOT:

主的语法:

PIVOT 
( 
    <aggregation function>(<column being aggregated>) 
FOR 
[<column that contains the values that will become column headers>] 
    IN ( [first pivoted column], [second pivoted column], 
    ... [last pivoted column]) 
) AS <alias for the pivot table> 

#1


4  

I think using [,] can solve it

我认为使用[,]可以解决这个问题

select id,pid
from #a
pivot(sum(id) for pid in([4721],[25])) as pvt

The syntax for PIVOT:

主的语法:

PIVOT 
( 
    <aggregation function>(<column being aggregated>) 
FOR 
[<column that contains the values that will become column headers>] 
    IN ( [first pivoted column], [second pivoted column], 
    ... [last pivoted column]) 
) AS <alias for the pivot table>