我想为某些不同类型的工作获取表格的一些聚合值。我可以将这 3 个查询合并为一个查询吗?
SELECT
COUNT(*) AS totaljobs,
SUM(filesize) AS totalfilesize
FROM Job
SELECT
COUNT(*) AS assignedjobs,
SUM(filesize) AS assignedjobs
FROM Job
JOIN Assignment ON Assignment.job_id = Job.id
SELECT
COUNT(*) AS unassignedjobs,
SUM(filesize) AS unassignedjobs
FROM Job
LEFT JOIN Assignment ON Assignment.job_id = Job.id
WHERE Assignment.job_id IS NULL
查询的行为与您预期的一样,例如totaljobs = assignedjobs + unassignedjobs
. 我觉得有办法在一个查询中更有效地做到这一点,但我不知道如何。