有人可以解释一下为什么会有效:
select val1, val2,
count(case when table2.someID in (1, 2, 3, 48967, 123456) then table2.someId end) as val3
from table1
join table2 on table1.someId = table2.someId
where blabla
group by val1, val2
order by val1
但是这个查询在这里:
select val1, val2,
count(case when table2.someID in (Select someId from table567) then table2.someId end) as val3
from table1
join table2 on table1.someId = table2.someId
where blabla
group by val1, val2
order by val1
给出错误:
无法对包含的表达式执行聚合函数 聚合或子查询。
(someId
是table2
中的PK
答案 0 :(得分:4)
好吧,如果它“无法对包含(...)子查询的表达式执行聚合函数”那么原因很简单:在第二个查询中你有子查询(Select someId from table567
)in汇总函数count
,首先你没有。
答案 1 :(得分:3)
您可以使用左连接而不是子查询来解决此问题。
select val1,
val2,
count(table567.someID) as val3
from table1
join table2 on table1.someId = table2.someId
left join table567 on table2.someID = table567.someID
where blabla
group by val1, val2
order by val1