简体   繁体   English

合并SQL中的UNION结果

[英]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 我得到以下信息:SQL语句的结果

SQL语句的结果

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. sql表完全相同。

This Is what I would like it to look like: 这就是我想要的样子:

在此处输入图片说明

A FULL OUTER JOIN would be ideal. FULL OUTER JOIN将是理想的。 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: 如果您要计算每个月不同项目的数量,请union all合并,然后将结果合并到更高的级别:

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;

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;

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM