2014-01-27 121 views
0

我試圖使用LEFT JOIN連接3個表,但我總是有錯誤。請幫幫我。在3個表中使用左連接

謝謝

,我想輸出:

Name   Date    Open    Close 
POS    01/01/2014   4     10 
ONE    01/02/2014   6     5 
... 
... 
... 

我的查詢:

SELECT 'Data' 
||','||TO_CHAR(D.DTIME_DAY,'MM/dd/yyyy') 
||','||NVL(o.CNT_OPENED,0) --as cnt_opened 
||','||NVL(c.CNT_CLOSED,0) --as cnt_closed 
||','||q.NAME 
FROM OWNER_DWH.DC_DATE d 
LEFT JOIN APP_ACCOUNT.OTRS_QUEUE q 
ON t.queue_id = q.id 
LEFT JOIN (
SELECT TRUNC(t.CREATE_TIME) AS report_date,count(*) AS cnt_opened 
FROM APP_ACCOUNT.OTRS_TICKET t 
WHERE t.CREATE_TIME BETWEEN SYSDATE -120 AND SYSDATE 
GROUP BY TRUNC(t.CREATE_TIME) 
) o ON d.DTIME_DAY=o.REPORT_DATE 
LEFT JOIN (
SELECT TRUNC(t.CLOSE_TIME) as report_date,count(*) AS cnt_closed 
FROM APP_ACCOUNT.OTRS_TICKET t 
WHERE t.CLOSE_TIME BETWEEN SYSDATE -120 AND SYSDATE 
GROUP BY TRUNC(t.CLOSE_TIME) 
) c ON D.DTIME_DAY=c.REPORT_DATE 
WHERE d.DTIME_DAY BETWEEN SYSDATE -120 AND TRUNC(SYSDATE) -1 
AND d.DTIME_DAY = TRUNC(d.DTIME_DAY) 
AND TRUNC(d.DTIME_DAY)= d.DTIME_DAY 
ORDER BY D.DTIME_DAY; 
+0

刪除第8行中的';'然後嘗試'ON t.queue_id = q.id' – Dba

+0

任何含錯誤的語言? – wruckie

+0

ORA-00904:「T」。「QUEUE_ID」:無效標識符@wruckie – user3098728

回答

1

你不必爲 「T」 的任何表的別名在你的第一個左連接。所以oracle不知道什麼是t.queue_id中的「t」,但是在加入條件下

ON t.queue_id = q.id

從代碼

SELECT 'Data' 
||','||TO_CHAR(D.DTIME_DAY,'MM/dd/yyyy') 
||','||NVL(o.CNT_OPENED,0) --as cnt_opened 
||','||NVL(c.CNT_CLOSED,0) --as cnt_closed 
||','||q.NAME 
FROM OWNER_DWH.DC_DATE d 
LEFT JOIN APP_ACCOUNT.OTRS_QUEUE q 
ON t.queue_id = q.id 
LEFT JOIN (
SELECT TRUNC(t.CREATE_TIME) AS report_date,count(*) AS cnt_opened 
FROM APP_ACCOUNT.OTRS_TICKET t 
WHERE t.CREATE_TIME BETWEEN SYSDATE -120 AND SYSDATE 
GROUP BY TRUNC(t.CREATE_TIME) 
) 

「T」 是該塊內定義的別名:現在

LEFT JOIN ( 
SELECT TRUNC(t.CREATE_TIME) AS report_date,count(*) AS cnt_opened 
FROM APP_ACCOUNT.OTRS_TICKET t 
WHERE t.CREATE_TIME BETWEEN SYSDATE -120 AND SYSDATE 
GROUP BY TRUNC(t.CREATE_TIME) 
) 

,作爲連接ON t.queue_id = q.id是otside該塊(括號外),則別名「t」在該點不可用。

因此錯誤。

+0

我把LEFT JOIN APP_ACCOUNT.OTRS_QUEUE q ON t.queue_id = q.id LEFT JOIN( SELECT TRUNC(t.CREATE_TIME後)AS REPORT_DATE,COUNT(*)AS cnt_opened FROM APP_ACCOUNT.OTRS_TICKET噸 其中t .CREATE_TIME在SYSDATE -120和SYSDATE之間 GROUP BY TRUNC(t.CREATE_TIME) )o ON d.DTIME_DAY = o.REPORT_DATE但我仍然有錯誤 – user3098728

+0

我已經進一步解釋了我的答案。希望你明白這一點。 查詢是由您或其他人撰寫的?因爲如果你有,那麼我懷疑你需要首先理解編程範圍的一些基本概念。 – Arnab