第一次查询:
select MONTHNAME(batchStartDate) AS MONTHS,
MONTH(batchStartDate) AS MONTHNUMBER,
SUM(totalCandidatesInBatch) AS CANDIDATES_ENROLLED
from batch
group by MONTHS
order by MONTHNUMBER;
第二次查询:
select MONTHNAME(resultApprovedOndate) AS MONTHS,
MONTH(resultApprovedOndate) AS MONTHNUMBER,
SUM(totalCertified) AS CANDIDATES_CERTIFIED
from batch
where totalPass is not null AND
totalFail is not null AND
resultApprovedOnDate<=curDate() and
resultApprovedOnDate is not null
group by MONTHS
order by MONTHNUMBER;
第3次查询:
select MONTHNAME(assessmentDate) AS MONTHS,
MONTH(assessmentDate) AS MONTHNUMBER,
(SUM(totalPass)+SUM(totalFail)) AS CANDIDATES_ASSESSED
from batch
where totalPass is not null AND
totalFail is not null AND
resultApprovedOnDate<=curDate() and
resultApprovedOnDate is not null
group by MONTHS
order by MONTHNUMBER;
正如您所看到的,这些查询具有公共列MONTHS和MONTHNUMBER,我想加入这三个查询的结果。 我尝试过UNION但是没有用。
任何人都可以告诉我发生了什么以及解决方案是什么?
OUTPUT 第一次查询
# MONTHS, MONTHNUMBER, CANDIDATES_ENROLLED
January, 1, 50
February, 2, 35
March, 3, 205
April, 4, 85
May, 5, 616
June, 6, 26
第二次查询
# MONTHS, MONTHNUMBER, CANDIDATES_CERTIFIED
January, 1, 34
April, 4, 72
July, 7, 484
August, 8, 30
第三次查询
# MONTHS, MONTHNUMBER, CANDIDATES_ASSESSED
February, 2, 101
March, 3, 134
May, 5, 32
July, 7, 75
August, 8, 75
September, 9, 150
October, 10, 75
November, 11, 77
December, 12, 152
我希望输出以表格
显示# MONTHS, MONTHNUMBER, CANDIDATES_ENROLLED, CANDIDATES_CERTIFIED, CANDIDATES_ASSESSED
并且如果一个表有一个行缺失,那么该值应该用零或null填充.Ex-在第三个查询输出中,1月缺失,那么评估的候选应该是0或null。
答案 0 :(得分:0)
看看这个链接: https://www.w3schools.com/sql/sql_join.asp
它会显示不同类型的联接(我假设您想要进行&#34;加入&#34;)。
Union用于将两个或多个集合组合在一起。
使用联接,您可以根据表格之间的相关列加入
答案 1 :(得分:0)
我认为这可以通过一个查询来完成:
select MONTHNAME(batchStartDate) AS `MONTHS`,
MONTH(batchStartDate) AS `MONTHNUMBER`,
SUM(totalCandidatesInBatch) AS `CANDIDATESE_ENROLLED`,
SUM(
IF(NOT(totalFail IS NULL) AND
NOT(totalFail IS NULL) AND
resultApprovedOnDate <= curDate() AND
NOT(resultApprovedOnDate is null),
totalCertified,
0
) AS `CANDIDATESE_CERTIFIED`,
SUM(
IF(NOT(totalFail IS NULL) AND
NOT(totalFail IS NULL) AND
resultApprovedOnDate <= curDate() AND
NOT(resultApprovedOnDate is null),
totalPass + totalFail,
0
) AS `CANDIDATESE_ASSESSED`
from batch
group by MONTHS
order by MONTHNUMBER;