0

有人可以解释一下为什么会这样:

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在PK中table2

4

2 回答 2

4

好吧,如果它“无法对包含 (...) 子查询的表达式执行聚合函数Select someId from table567”,那么原因很简单:在第二个查询中,聚合函数中有子查询 ( ),count而第一个则没有。

于 2012-06-18T07:45:26.353 回答
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
于 2012-06-18T07:54:00.700 回答