wbrandt
wbrandt

Reputation: 149

Merging together results from a UNION in sql

I am trying to combine the results of a Union from

SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total 
FROM projects
WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON'
GROUP BY MONTH(terms)
UNION
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total 
FROM archive
WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON'
GROUP BY MONTH(terms)

I get the following: RESULTS FROM THE SQL STATEMENT

RESULTS FROM THE SQL STATEMENT

I am trying to make it so the total will be the combination of the multiple instances of the month. The sql tables are exactly the same.

This Is what I would like it to look like:

enter image description here

Upvotes: 0

Views: 62

Answers (4)

Spliid
Spliid

Reputation: 531

You can try creating a sum expression on the entire query.

SELECT month, SUM (total) FROM
(SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total FROM projects WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON' GROUP BY MONTH(terms)
UNION
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total FROM archive WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON' GROUP BY MONTH(terms))
GROUP BY month

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269803

A FULL OUTER JOIN would be ideal. But in your case, let's do two levels of aggregation:

SELECT month, MAX(total_projects) as total_projects, MAX(total_archive) as total_archive
FROM ((SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total_projects, 0 as total_archive
       FROM projects
       WHERE terms >= '2017/01/01' AND Building_designer = 'SOMEPERSON'
       GROUP BY MONTH(terms)
      ) UNION ALL
      (SELECT MONTHNAME(terms) AS month, 0, COUNT(DISTINCT project_num
       FROM archive
       WHERE terms >= '2017/01/01' AND Building_designer = 'SOMEPERSON'
       GROUP BY MONTH(terms)
      )
     ) pa
GROUP BY month
ORDER BY month;

EDIT:

Oops. You only want one column. If you want to count the number of distinct projects for each month, then do a union all and then combine the results at the next higher level:

SELECT month, COUNT(DISTINCT project_num) as total
FROM ((SELECT MONTHNAME(terms) AS month, project_num
       FROM projects
       WHERE terms >= '2017/01/01' AND Building_designer = 'SOMEPERSON'
      ) UNION ALL
      (SELECT MONTHNAME(terms) AS month, project_num
       FROM archive
       WHERE terms >= '2017/01/01' AND Building_designer = 'SOMEPERSON'
      )
     ) pa
GROUP BY month
ORDER BY month;

Upvotes: 1

RoMEoMusTDiE
RoMEoMusTDiE

Reputation: 4824

transform into a derived table, put an alias then aggregate

select 
x.month,
sum(x.total) [Total]
from (
SELECT
  MONTHNAME(terms) AS month,
  COUNT(DISTINCT project_num) AS total
FROM projects
WHERE terms >= '2017/01/01'
AND Building_designer = 'SOMEPERSON'
GROUP BY MONTH(terms)
UNION
SELECT
  MONTHNAME(terms) AS month,
  COUNT(DISTINCT project_num) AS total
FROM archive
WHERE terms >= '2017/01/01'
AND Building_designer = 'SOMEPERSON'
GROUP BY MONTH(terms)
) x

group by x.month

Upvotes: 0

Mark Giaconia
Mark Giaconia

Reputation: 3953

A quick thought would be to just do something like this. You essentially want to sum the counts from each table.

select month, sum(total) from 
(
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total FROM projects WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON' GROUP BY MONTH(terms)
UNION
SELECT MONTHNAME(terms) AS month, COUNT(DISTINCT project_num) as total FROM archive WHERE terms >= '2017/01/01' AND Building_designer='SOMEPERSON' GROUP BY MONTH(terms)
) group by month;

Upvotes: 0

Related Questions