2013-12-19 94 views
0

我想選擇誰已經比平均總時間,任何人都已經工作,一個陣營的Oracle SQL子查詢

表是人與計劃工作較少的人

這是我到目前爲止有:

SELECT fname AS "First Name", lname AS "Last Name", 
SUM((end_time - start_time) * 24)   
FROM person JOIN schedule USING (person_ID) 
GROUP BY fname 
HAVING SUM((end_time - start_time) * 24) < (
SELECT AVG(SUM((end_time - start_time) * 24)) FROM schedule);` 

人有FNAME,LNAME和PERSON_ID 時間表sched_id,s_date,START_TIME,END_TIME和PERSON_ID

謝謝!

收到此錯誤:

Error starting at line 18 in command: 
    SELECT fname AS "First Name", lname AS "Last Name", 
    SUM((end_time - start_time) * 24)  
    FROM person JOIN schedule USING (person_ID) 
    GROUP BY fname 
    HAVING SUM((end_time - start_time) * 24) < (
    SELECT AVG(SUM((end_time - start_time) * 24)) FROM schedule) 
    Error at Command Line:18 Column:31 
    Error report: 
    SQL Error: ORA-00979: not a GROUP BY expression 
    00979. 00000 - "not a GROUP BY expression" 
    *Cause:  
    *Action: 
+0

您必須將select中的所有非聚合列包含在group by子句中。 – OldProgrammer

回答

1

本集團需要包括在你的SELECT語句中的所有非聚集的領域(所以一切除了SUM()在這種情況下)。

嘗試將其更改爲:

GROUP BY fname, lname 
0

當使用聚合函數,如SUM,您將需要通過包括該組中的所有非集合屬性:

SELECT fname AS "First Name", lname AS "Last Name", 
SUM((end_time - start_time) * 24)   
FROM person JOIN schedule USING (person_ID) 
GROUP BY fname, lname --this is changed 
HAVING SUM((end_time - start_time) * 24) < (
SELECT AVG(SUM((end_time - start_time) * 24)) FROM schedule);` 
+0

現在說的是 命令行錯誤:27列:16 錯誤報告: SQL錯誤:ORA-00978:沒有GROUP BY的嵌套組函數 00978. 00000 - 「沒有GROUP BY的嵌套組函數」 – user3038436

0

我想解決此問題的最佳方法是使用分析函數:

select "First Name", "Last Name", hours 
from (SELECT fname AS "First Name", lname AS "Last Name", 
      SUM((end_time - start_time) * 24) as hours, 
      avg(SUM((end_time - start_time) * 24)) over() as avghours 
     FROM person JOIN 
      schedule 
      USING (person_ID) 
     GROUP BY fname, lname 
    ) t 
WHERE hours < avghours; 

報告的特定問題Oracle是avg(sum())不允許用於group by(除非,如上所述,avg()實際上是一個分析函數)。