如何将两个不同的查询组合成一个输出

时间:2014-02-26 14:57:07

标签: sql oracle

我是SQL查询的新手,并且已被抛入深层!

我有两个需要合并的查询。一个执行三次计算(总和为WGHours,计为WGCount,总和/计为WGAvg)并将输出分组为2列:工作组和优先级。另一个执行相同的计算(PHours,PCOunt,PAvg但只有一列分组 - 优先级。这样我就可以找到每个优先级的总数,这样我最终可以划分 WGAvg / PAvg 产生 a%

我可以单独创建两个查询,但需要将两者的输出合并在一起,以便我可以在一个表中使用它们。我已经尝试过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;           

2 个答案:

答案 0 :(得分: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表别名。

答案 1 :(得分: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 ... ;