分组依据:ORA-00937:不是单组组功能

时间:2013-04-25 05:04:14

标签: sql oracle group-by having

为什么此查询无法正常运行?

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

谢谢,

2 个答案:

答案 0 :(得分:4)

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

如果您想选择这些列,但仅针对某些关键字studentid的{​​{1}},您可以这样做:

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 ==

答案 1 :(得分:1)

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

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

及其having count(*) = 1。不是having count(*) ==1