1

为什么这个查询不能正常工作?

SELECT e.* 
FROM enrolled e 
FULL OUTER JOIN student s ON e.studentid = s.sid 
WHERE ((e.quarter = 'Fall') OR (e.quarter = 'Spring')) 
GROUP BY e.studentid 
HAVING count(e.studentid) == 1;

发生的错误:

ORA-00937: 不是单组组函数 00937. 00000 - “不是单组组函数” *原因:*操作:行错误:1 列:8

谢谢,

4

2 回答 2

4

正如 Rene 所说,您不能选择不在 group by 子句中的列。

如果您想选择这些列,但仅适用于studentid具有某些标准的 s,count(*) = 2您可以这样做:

select *
from
(SELECT e.*, count(*) over (partition by e.studentid)  cnt
FROM enrolled e 
FULL OUTER JOIN student s ON e.studentid = s.sid 
WHERE ((e.quarter = 'Fall') OR (e.quarter = 'Spring')) 
)
where cnt = 2

或者

SELECT e.*, count(*) over (partition by e.studentid)  cnt
FROM enrolled e 
FULL OUTER JOIN student s ON e.studentid = s.sid 
WHERE ((e.quarter = 'Fall') OR (e.quarter = 'Spring')) 
  AND (select count(*) from enrolled i where i.studentid = e.studentid) = 2

无论如何,你不能==只做=

于 2013-04-25T06:02:35.950 回答
1

当您使用“分组依据”时,您只能选择“分组依据”中使用的列。

您正在选择 e.*。将其更改为 e.studentid。

而它的having count(*) = 1. 不是having count(*) ==1

于 2013-04-25T05:46:57.013 回答