我有以下查询:
SELECT employee,department,count(*) AS sum FROM items
WHERE ((employee = 1 AND department = 2) OR
(employee = 3 AND department = 4) OR
(employee = 5 AND department = 6) OR
([more conditions with the same structure]))
AND available = true
GROUP BY employee, department;
如果一对“employee-department”没有项目,则查询不返回任何内容。我希望它返回零:
employee | department | sum
---------+------------+--------
1 | 2 | 0
3 | 4 | 12
5 | 6 | 1234
编辑1
看起来这是不可能的,正如 Matthew PK在他对类似问题的回答中所解释的那样。我错误地假设 Postgres 可以以某种方式从 WHERE 子句中提取缺失值。
编辑2
有一些技巧是可以的。:) 感谢欧文·布兰德施泰特!