我是新來的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;
哪個dbms? Oracle或sql服務器? – JiggsJedi