title: How to combine two query grouped differently into one output
body: <p>I'm new to SQL queries and have been thrown in the deep end!</p>
  
  <p>I have two queries which I need to merge together. One performs three calculations <em>(sum as WGHours, count as WGCount, sum/count as WGAvg)</em> and groups the output by 2 columns: <em>work group and priority.</em> The other performs the same calculations <em>(PHours, PCOunt, PAvg but only groups by one column - priority</em>. This is so I can find the total figure for each priority so i can finally divide <strong>WGAvg / PAvg</strong> to produce <strong>a %</strong>.</p>
  
  <p>I can create both queries individually, but need to merge the output of both together so that I can use them in one table. I've tried UNION ALL but it doesn't produce the output I need, which is:</p>
  
  <pre><code>WORK_GROUP, PRIORITY, WGHOURS, WGCount, WGAvg, PHours, PCount, PAvg
  </code></pre>
  
  <p>Any idea how I do this? I've tried using a solution I found on here but get an <strong>oracle error telling me that the command is no properly ended</strong>:</p>
  
  <pre><code>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 &gt; 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 &gt;= TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE, 'MONTH'), - 1), 'yyyymmdd')) AND 
             (CORVU.MSV620.CLOSED_DT &lt;= 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;           
  </code></pre>