需要在oracle sql中包含一个带有组的名称

时间:2015-09-29 19:56:40

标签: oracle

我这里有一些代码,

`SELECT a.Officer_ID, Count(Crime_ID)
FROM Officers a inner Join Crime_Officers b on a.Officer_ID =     b.Officer_ID
Group by a.Officer_ID
Having (Count(Crime_ID) > (select avg(distinct(Count(Crime_ID))) 
                      From Crime_Officers
                      Group by officer_Id));`

它输出这个,

`OFFICER_ID COUNT(CRIME_ID)
---------- ---------------
111115               9 `

它非常酷,但是我的任务只需要表A中的军官姓氏。我使用Officer_ID和Count尝试它以确保代码正常工作。当我这样试试时,

`SELECT Last
FROM Officers a inner Join Crime_Officers b on a.Officer_ID = b.Officer_ID
Group by a.Officer_ID
Having (Count(Crime_ID) > (select avg(distinct(Count(Crime_ID))) 
                      From Crime_Officers
                      Group by officer_Id));`

我收到此错误,

`Error starting at line 1 in command:
SELECT Last
FROM Officers a inner Join Crime_Officers b on a.Officer_ID = b.Officer_ID
Group by a.Officer_ID
Having (Count(Crime_ID) > (select avg(distinct(Count(Crime_ID))) 
                      From Crime_Officers
                      Group by officer_Id))
Error at Command Line:1 Column:8
Error report:
SQL Error: ORA-00979: not a GROUP BY expression
00979. 00000 -  "not a GROUP BY expression"
*Cause:    
*Action:`

任何人都可以帮助或解释我失踪/做错的事吗?

1 个答案:

答案 0 :(得分:0)

如果a)它在GROUP BY中,b)它是从GROUP BY列派生的,或者c)它是聚合函数的结果,你只能选择一列。 MAX / MIN。

在您的情况下,每个Officer_ID应该只有一个Last值。

只需将GROUP BY更改为:

GROUP BY a.Officer_ID, a.Last