2014-09-02 60 views
0

我來自一個MySQL背景,我用下面的查詢有問題:ORACLE集團通過與重複

SELECT DISTINCT agenda.idagenda AS "ID_SERVICE", agenda.name AS "ID_SERVICE_NAME", specialities.id AS "ID_DEPARTMENT", specialities.name AS "ID_DEPARTMENT_NAME", 
       supervisor.clients_waiting AS "CWaiting", 
       (CASE WHEN supervisor.clients_resent_waiting_area IS NULL THEN 0 ELSE supervisor.clients_resent_waiting_area END) AS "CWaiting_Resent_Area", 
       supervisor.clients_attending AS "CAttending", 
       supervisor.clients_attended AS "CAttended", 
       (SELECT MAX(ROUND((SYSDATE-core.supervisor_time_data.time_attending)*86400)) FROM dual) AS "MTA", 
       (SELECT MAX(ROUND((SYSDATE-core.supervisor_time_data.time_waiting)*86400)) FROM dual) AS "MTE", 
       (SELECT SUM(SYSDATE-supervisor_time_data.time_attending)*86400 FROM dual)/(SELECT supervisor.clients_attending FROM dual) AS "TMA", 
       (SELECT SUM(SYSDATE-supervisor_time_data.time_waiting)*86400 FROM dual)/(SELECT supervisor.clients_waiting FROM dual) AS "TME", 
       supervisor.tme_accumulated AS "TME_ACCUMULATED", 
       supervisor.tma_accumulated AS "TMA_ACCUMULATED", 
       (CASE WHEN agenda.alarm_cee IS NULL THEN 0 ELSE agenda.alarm_cee END) AS "ALARM_CEE", 
       (CASE WHEN agenda.alarm_mte IS NULL THEN 0 ELSE agenda.alarm_mte END) AS "ALARM_MTE", 
       (CASE WHEN agenda.alarm_mta IS NULL THEN 0 ELSE agenda.alarm_mta END) AS "ALARM_MTA", 
       (CASE WHEN agenda.alarm_tme IS NULL THEN 0 ELSE agenda.alarm_tme END) AS "ALARM_TME" 
       FROM CORE.supervisor 
       LEFT JOIN CORE.supervisor_time_data ON supervisor_time_data.id_service = supervisor.id_service 
       LEFT JOIN CORE.agenda ON supervisor.id_service = agenda.id 
       LEFT JOIN CORE.specialities ON agenda.idspeciality = specialities.id 

      WHERE supervisor.booked_or_sequential = 1 

      GROUP BY agenda.idagenda, agenda.name, supervisor.id_service, specialities.id, specialities.name, supervisor.clients_waiting, supervisor.clients_resent_waiting_area, 
      supervisor.clients_attending, supervisor.clients_attended, 
      supervisor_time_data.time_attending, supervisor_time_data.time_waiting, 
      supervisor.tme_accumulated, supervisor.tma_accumulated, agenda.alarm_cee, agenda.alarm_mte,agenda.alarm_mta,agenda.alarm_tme; 

它應該返回兩個記錄,而是它返回四強。 ID_SERIVE正在返回3個具有相同值的記錄。

"ID_SERVICE" "ID_SERVICE_NAME" "ID_DEPARTMENT" "ID_DEPARTMENT_NAME" "CWaiting" "CWaiting_Resent_Area" "CAttending" "CAttended" "MTA" "MTE" "TMA" "TME" "TME_ACCUMULATED" "TMA_ACCUMULATED" "ALARM_CEE" "ALARM_MTE" "ALARM_MTA" "ALARM_TME" 
"DR" "DR" 1 "SECUENCIALES" 1 0 1 1 5504  5504  21 109 0 0 0 0 
"DR" "DR" 1 "SECUENCIALES" 1 0 1 1  1590   1590.000000000000000000000000000000000002 21 109 0 0 0 0 
"DR" "DR" 1 "SECUENCIALES" 1 0 1 1     21 109 0 0 0 0 
"TRAU" "TRAU" 1 "SECUENCIALES" 1 0 0 0  1567  1567.000000000000000000000000000000000002 0 0 0 0 0 0 

我在做什麼錯?

感謝

+0

你正在按不同的值進行分組 - 'id_servie'是相同的,但其他的不是。如果它只爲每個服務顯示一行,那麼它應該具有哪個MTA值?您可以按某些列進行分組,並對其他人進行彙總(總和,最小值,最大值等),但不清楚您真正想要的。看起來你在'group by'子句中只是有太多的東西。你想要每個服務的最大MTA值? (並且雙重所有子查詢有什麼區別?) – 2014-09-02 09:30:12

+0

MTA值應該是MAX。在Oracle中,group by子句需要包含所有的SELECTS。如果我忽略其中一個,我會得到錯誤「不是GROUP BY表達式」 – Antoni 2014-09-02 09:35:24

+0

「group by」需要包含所有不是聚合函數的選擇列表項,這是一個重要的區別。 – 2014-09-02 09:42:25

回答

0

你似乎是包括所有你在group by有趣的列,而不是聚集正確;可能你已經通過試驗和錯誤達到了這一點,因爲你試圖解決來自未被分組的列的錯誤。您不需要所有列子句中的子選擇。

未經檢驗的,因爲我們沒有你的表或原始數據,但它看起來像你想要的東西,如:

SELECT agenda.idagenda AS "ID_SERVICE", 
    agenda.name AS "ID_SERVICE_NAME", 
    specialities.id AS "ID_DEPARTMENT", 
    specialities.name AS "ID_DEPARTMENT_NAME", 
    supervisor.clients_waiting AS "CWaiting", 
    NVL(supervisor.clients_resent_waiting_area, 0) AS "CWaiting_Resent_Area", 
    supervisor.clients_attending AS "CAttending", 
    supervisor.clients_attended AS "CAttended", 
    MAX(ROUND((SYSDATE - supervisor_time_data.time_attending)*86400)) AS "MTA", 
    MAX(ROUND((SYSDATE - supervisor_time_data.time_waiting)*86400)) AS "MTE", 
    SUM(SYSDATE - supervisor_time_data.time_attending)*86400 
    /supervisor.clients_attending AS "TMA", 
    SUM(SYSDATE - supervisor_time_data.time_waiting)*86400 
    /supervisor.clients_waiting AS "TME", 
    supervisor.tme_accumulated AS "TME_ACCUMULATED", 
    supervisor.tma_accumulated AS "TMA_ACCUMULATED", 
    NVL(agenda.alarm_cee, 0) AS "ALARM_CEE", 
    NVL(agenda.alarm_mte, 0) AS "ALARM_MTE", 
    NVL(agenda.alarm_mta, 0) AS "ALARM_MTA", 
    NVL(agenda.alarm_tme, 0) AS "ALARM_TME" 
FROM CORE.supervisor 
LEFT JOIN CORE.supervisor_time_data 
    ON supervisor_time_data.id_service = supervisor.id_service 
LEFT JOIN CORE.agenda ON supervisor.id_service = agenda.id 
LEFT JOIN CORE.specialities ON agenda.idspeciality = specialities.id 
WHERE supervisor.booked_or_sequential = 1 
GROUP BY agenda.idagenda, agenda.name, supervisor.id_service, specialities.id, 
    specialities.name, supervisor.clients_waiting, 
    supervisor.clients_resent_waiting_area, supervisor.clients_attending, 
    supervisor.clients_attended, supervisor.tme_accumulated, 
    supervisor.tma_accumulated, agenda.alarm_cee, 
    agenda.alarm_mte,agenda.alarm_mta,agenda.alarm_tme; 

那麼具體的,supervisor_time_data.time_waitingsupervisor_time_data.time_attending不需要是在group by,因爲它們是合計使用。

我已將case支票替換爲nvl,因爲它更短;如果你喜歡這種情況,情況很好。

+0

相同的錯誤,ORA-00979:不是GROUP BY表達式 00979. 00000 - 「不是GROUP BY表達式」 *原因: *操作: 錯誤在行:15列:3.我不知道nvl ,謝謝 – Antoni 2014-09-02 09:48:30

+1

@Anton - 我將錯誤的專欄排除在小組之外;剪貼錯誤,抱歉。我現在沒有看到這個代碼的錯誤。 – 2014-09-02 09:53:07

+0

它的工作原理!謝謝! – Antoni 2014-09-02 09:54:39