select out.a,out.b from out
pivot (count([event]) for [date] in ([Mon],[Tues])) as event_count
group by out.a,out.b
执行此查询时出现以下错误:
多部分标识符out.a,out.b无法绑定
答案 0 :(得分:7)
删除GROUP BY
子句以及列out
和a
的别名b
,如下所示:
select a, b, [Mon], [Tues]
from out
pivot
(
count([event])
for [date] in ([Mon],[Tues])
) as event_count;
请注意:使用PIVOT
表运算符时,您不需要显式指定分组元素,在您的情况a, b
或源表中,您需要从查询和源表中删除它。 PIVOT
运算符会自动计算出您想要的列GROUP BY
,这些列是date
或汇总元素event
中未指定的列。在你的情况下,其余的将是分组列。
这就是为什么你必须明确列出列的名称而不是FROM out
,因为表out
包含a, b, date, event
以外的列。在这种情况下,您必须这样做:
select a, b, [Mon], [Tues]
from
(
SELECT a, b, "date", "event"
FROM out
) t
pivot
(
count([event])
for [date] in ([Mon],[Tues])
) as event_count;
答案 1 :(得分:2)
听起来你需要在[星期一]和[星期二]
上替换out.aSELECT [Mon], [Tues]
FROM out
PIVOT (
COUNT([event]) FOR [date] in ([Mon],[Tues])
) AS event_count
SQLFiddle上的演示
答案 2 :(得分:1)
我不确定您的查询,但可能是您正在寻找的
Select a,b, [Mon],[Tues] from
(
Select a,b,date,event from out
group by a,b,date,event
)L
PIVOT
(
count(event) for [date] in ([Mon],[Tues])
) event_count