[英]Query with subquery needs to return all rows from original query (even when subquery comes back empty)
I am trying to write an SQL query that returns the maximum value scored on a test per employee, with each test in its own column. 我正在尝试编写一个SQL查询,该查询返回每个员工在测试中得分的最大值,每个测试都在其自己的列中。
So far, I've come up with: 到目前为止,我想出了:
SELECT E.id,
Concat(E.firstname, ' ', E.middlename, ' ', E.lastname) AS FullName,
score1.maxscore,
score2.maxscore
FROM employees AS E,
(SELECT empid,
Max(Cast(score AS DECIMAL)) AS MaxScore
FROM filledsoc
WHERE socid = '87bf5fba-5702-4304-869c-8707956d34d0'
GROUP BY empid) AS score1,
(SELECT empid,
Max(Cast(score AS DECIMAL)) AS MaxScore
FROM filledsoc
WHERE socid = '340363d9-e9a2-478b-b819-d3a56d337561'
GROUP BY empid) AS score2
WHERE E.id = score1.empid
AND E.id = score2.empid
This is just a testquery, the final query will be built dynamically. 这只是一个testquery,最终查询将动态建立。 My problem is that the query only returns results where all columns have a value. 我的问题是查询仅返回所有列都有值的结果。
If one of the subquerys comes back empty, the employee is skipped alltogether. 如果子查询之一返回为空,则将雇员全部跳过。
What I need is a query that returns all rows from table 'employees', and fills in score1.maxscore and score2.maxscore only when it exists. 我需要的是一个查询,该查询返回表“ employees”中的所有行,并且仅在存在时才填写score1.maxscore和score2.maxscore。 IF it doesn't '0' or nothing would be fine... 如果不是'0'或什么都没有...
Any help on this would be appreciated. 任何帮助,将不胜感激。
Michel 米歇尔
SELECT E.ID, CONCAT(E.FirstName, ' ', E.MiddleName, ' ', E.LastName) AS FullName,
(
SELECT MAX(CAST(F1.Score AS DECIMAL))
FROM FilledSOC AS F1
WHERE F1.SocID = '87bf5fba-5702-4304-869c-8707956d34d0'
AND E.ID = F1.EmpID
) AS Score_1,
(
SELECT MAX(CAST(F2.Score AS DECIMAL))
FROM FilledSOC AS F2
WHERE F2.SocID = '340363d9-e9a2-478b-b819-d3a56d337561'
AND E.ID = F2.EmpID
) AS Score_2
FROM Employees AS E
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.