2014-02-26 77 views
0

我是新來的SQL查詢,並且已經被拋入了深層!如何將兩個不同的查詢組合成不同的輸出到一個輸出

我有兩個需要合併在一起的查詢。一個執行三個計算(總和爲WGHours,計爲WGCount,總和/計數爲WGAvg)並將輸出分爲2列:工作組和優先級。其他執行相同的計算(PHours,PCOunt,PAVG而是由一列僅基 - 優先這是這樣我就可以找到針對每個優先級的總圖中,所以我可以最後劃分WGAvg/PAVG以產生一個%

我可以單獨創建兩個查詢,但需要將兩者的輸出合併在一起,以便我可以在一個表中使用它們。我試過UNION ALL,但它不會生成我需要的輸出,這是:

WORK_GROUP, PRIORITY, WGHOURS, WGCount, WGAvg, PHours, PCount, PAvg 

任何想法我如何做到這一點?我試過使用一個解決辦法,我在這裏找到,但得到的Oracle錯誤,告訴我該命令沒有正確地結束

SELECT One.OPriority, 
     WorkGroup, 
     AverageHours, 
     WoCount, 
     Divis 
FROM (Select CORVU.MSV620.ORIG_PRIORITY as OPriority, 
      CORVU.MSV620.WORK_GROUP as WorkGroup, 
      SUM(CORVU.MSV621.ACT_LAB_HRS) AS AverageHours, 
      COUNT(CORVU.MSV621.WORK_ORDER) AS WoCount, 
      CAST(SUM(CORVU.MSV621.ACT_LAB_HRS)/COUNT(CORVU.MSV621.WORK_ORDER) AS DECIMAL (38, 2)) AS Divis 
    FROM CORVU.MSV620, CORVU.MSV621 
    WHERE CORVU.MSV620.WORK_ORDER = CORVU.MSV621.WORK_ORDER 
    GROUP BY CORVU.MSV620.ORIG_PRIORITY, CORVU.MSV620.WORK_GROUP) AS one 
JOIN (
     SELECT CORVU.MSV620.ORIG_PRIORITY as OPriority, 
     CAST(SUM(CORVU.MSV621.ACT_LAB_HRS)/COUNT(CORVU.MSV621.WORK_ORDER) AS DECIMAL (38, 2)) AS DivisOP 
     FROM  CORVU.MSV620, CORVU.MSV621 
     WHERE CORVU.MSV620.WORK_ORDER = CORVU.MSV621.WORK_ORDER AND (CORVU.MSV620.WORK_GROUP IN ('ENGA01', 'ENGA02', 'ENGA04', 'ENGA05', 'ENGA06', 
      'ENGA08')) AND (CORVU.MSV620.ACCOUNT_CODE LIKE '%B01%') AND (CORVU.MSV621.ACT_LAB_HRS > 0) AND (NOT (CORVU.MSV620.STD_JOB_NO IN ('ELE995', 
      'MEC995', 'ICA995'))) AND (CORVU.MSV620.ORIG_PRIORITY IN ('CH', 'CS', 'T0', 'T1', 'T2', 'T3', 'T4')) AND 
      (CORVU.MSV620.CLOSED_DT >= TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE, 'MONTH'), - 1), 'yyyymmdd')) AND 
      (CORVU.MSV620.CLOSED_DT <= TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE + 30, 'MONTH') - 1, - 1), 'yyyymmdd')) 
     GROUP BY CORVU.MSV620.ORIG_PRIORITY) as two 
WHERE  one.CORVU.MSV620.ORIG_PRIORITY = two.CORVU.MSV620.ORIG_PRIORITY;   
+0

哪個dbms? Oracle或sql服務器? – JiggsJedi

回答

0

嘗試使用第二查詢作爲一個字段使用(子)選擇,就像這樣:

select a.name, a.address, sum(a.xxx), 
(select avg(b.amount) from costs b where b.id=a.id) 
from clients a 
where ... ; 
2

在您的查詢中,您正在使用join。這然後期望on子句而不是where子句。這可能會解決您的問題:

SELECT One.OPriority, 
     WorkGroup, 
     AverageHours, 
     WoCount, 
     Divis 
FROM (Select CORVU.MSV620.ORIG_PRIORITY as OPriority, 
      CORVU.MSV620.WORK_GROUP as WorkGroup, 
      SUM(CORVU.MSV621.ACT_LAB_HRS) AS AverageHours, 
      COUNT(CORVU.MSV621.WORK_ORDER) AS WoCount, 
      CAST(SUM(CORVU.MSV621.ACT_LAB_HRS)/COUNT(CORVU.MSV621.WORK_ORDER) AS DECIMAL (38, 2)) AS Divis 
    FROM CORVU.MSV620, CORVU.MSV621 
    WHERE CORVU.MSV620.WORK_ORDER = CORVU.MSV621.WORK_ORDER 
    GROUP BY CORVU.MSV620.ORIG_PRIORITY, CORVU.MSV620.WORK_GROUP) one 
------------------------------------------------------------------^ 
JOIN (
     SELECT CORVU.MSV620.ORIG_PRIORITY as OPriority, 
     CAST(SUM(CORVU.MSV621.ACT_LAB_HRS)/COUNT(CORVU.MSV621.WORK_ORDER) AS DECIMAL (38, 2)) AS DivisOP 
     FROM  CORVU.MSV620, CORVU.MSV621 
     WHERE CORVU.MSV620.WORK_ORDER = CORVU.MSV621.WORK_ORDER AND (CORVU.MSV620.WORK_GROUP IN ('ENGA01', 'ENGA02', 'ENGA04', 'ENGA05', 'ENGA06', 
      'ENGA08')) AND (CORVU.MSV620.ACCOUNT_CODE LIKE '%B01%') AND (CORVU.MSV621.ACT_LAB_HRS > 0) AND (NOT (CORVU.MSV620.STD_JOB_NO IN ('ELE995', 
      'MEC995', 'ICA995'))) AND (CORVU.MSV620.ORIG_PRIORITY IN ('CH', 'CS', 'T0', 'T1', 'T2', 'T3', 'T4')) AND 
      (CORVU.MSV620.CLOSED_DT >= TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE, 'MONTH'), - 1), 'yyyymmdd')) AND 
      (CORVU.MSV620.CLOSED_DT <= TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE + 30, 'MONTH') - 1, - 1), 'yyyymmdd')) 
     GROUP BY CORVU.MSV620.ORIG_PRIORITY) two 
---------------------------------------------^ 
     on one.CORVU.MSV620.ORIG_PRIORITY = two.CORVU.MSV620.ORIG_PRIORITY; 
------^ was WHERE 

編輯:

我也去掉了as(因爲你已經在註釋中說明)。 Oracle不允許as用於表別名。

+0

感謝您的幫助 - 但我仍然收到一個錯誤,告訴我該命令未正確結束。這來自Oracle SQl開發人員15. –

+0

@NeilDeeley。 。 。在這種情況下,首先單獨運行每個子查詢以確保它們在語法上是正確的。結合它們的結構看起來不錯。 –

+0

@NeilDeeley。 。 。我可能猜測你想要一個「左外連接」或「完全外連接」。 –

相關問題