MSSQL PIVOT - 语法不正确。期待 ID 或 QUOTED_ID
MSSQL PIVOT - Incorrect syntax. Expecting ID or QUOTED_ID
代码如下
select orgUnitNum, [2020-12-25], [2020-12-26], [2020-12-27], [2020-12-28], [2020-12-29], [2020-12-30], [2020-12-31], [2021-01-01]
FROM (select * from @myTable) as source
PIVOT (
max(logincount)
FOR [logindate] IN ( [2020-12-25], [2020-12-26], [2020-12-27], [2020-12-28], [2020-12-29], [2020-12-30], [2020-12-31], [2021-01-01])
) as pivot;
产生以下错误:
Incorrect syntax near 'pivot'. Expecting ID, or QUOTED_ID.
谢天谢地,我没有看到任何语法错误。
你能发现什么吗?
您用作 table 别名的第二个 'pivot' 就是问题所在。应该使用非关键字作为 table 别名,或将其定界为 [pivot]
.
select orgUnitNum, [2020-12-25], [2020-12-26], [2020-12-27], [2020-12-28], [2020-12-29], [2020-12-30], [2020-12-31], [2021-01-01]
FROM (select * from @myTable) as source
PIVOT (
max(logincount)
FOR [logindate] IN ( [2020-12-25], [2020-12-26], [2020-12-27], [2020-12-28], [2020-12-29], [2020-12-30], [2020-12-31], [2021-01-01])
) as pivoted;
代码如下
select orgUnitNum, [2020-12-25], [2020-12-26], [2020-12-27], [2020-12-28], [2020-12-29], [2020-12-30], [2020-12-31], [2021-01-01]
FROM (select * from @myTable) as source
PIVOT (
max(logincount)
FOR [logindate] IN ( [2020-12-25], [2020-12-26], [2020-12-27], [2020-12-28], [2020-12-29], [2020-12-30], [2020-12-31], [2021-01-01])
) as pivot;
产生以下错误:
Incorrect syntax near 'pivot'. Expecting ID, or QUOTED_ID.
谢天谢地,我没有看到任何语法错误。
你能发现什么吗?
您用作 table 别名的第二个 'pivot' 就是问题所在。应该使用非关键字作为 table 别名,或将其定界为 [pivot]
.
select orgUnitNum, [2020-12-25], [2020-12-26], [2020-12-27], [2020-12-28], [2020-12-29], [2020-12-30], [2020-12-31], [2021-01-01]
FROM (select * from @myTable) as source
PIVOT (
max(logincount)
FOR [logindate] IN ( [2020-12-25], [2020-12-26], [2020-12-27], [2020-12-28], [2020-12-29], [2020-12-30], [2020-12-31], [2021-01-01])
) as pivoted;