我有一张如下表 -
[ID] [PID]
1 4721
2 25
3 4721
我使用下面提到的查询创建了一个Pivot -
select id,pid
from #a
pivot(sum(id) for pid in(4721,25)) as pvt
但我在上面的代码中收到错误 - '4721'附近的语法不正确。我无法弄清楚原因。你能告诉我我失踪的地方吗?我希望结果表像 -
[4721] [25]
4, 2
答案 0 :(得分:4)
我认为使用[
,]
可以解决它
select id,pid
from #a
pivot(sum(id) for pid in([4721],[25])) as pvt
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>