Mysql 合并sql中的联合结果
我正试图把来自英国的工会的结果结合起来Mysql 合并sql中的联合结果,mysql,sql,Mysql,Sql,我正试图把来自英国的工会的结果结合起来 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 p
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)
我得到以下结果:来自SQL语句的结果
我正在努力使它的总数将是一个月的多个实例的组合。
sql表完全相同
这就是我希望它看起来的样子:
一个快速的想法就是做这样的事情。您基本上希望对每个表的计数求和
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;
转换为派生表,放置别名,然后聚合
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
一个完整的外部连接将是理想的。但在您的情况下,让我们进行两个级别的聚合:
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;
编辑:
哎呀。您只需要一列。如果要计算每个月不同项目的数量,请执行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;
可以尝试在整个查询上创建求和表达式
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
问题的所有要点都需要出现在问题本身中,而不是出现在外部站点上。不清楚您是希望两个表之间存在差异,还是希望两个表中的差异值的总和存在差异。假设使用UNION All手头的数据质量是安全的?