我知道我已经很接近了,它正在杀死我。需要一双新的眼睛…

SELECT
first_name,
COUNT(*) FROM
(SELECT first_name, job_status FROM typesetting
LEFT JOIN employees ON typesetting.employees_id = employees.id
LEFT JOIN job_status ON typesetting.job_status_id = job_status.id
WHERE job_status NOT LIKE 'Archived'
ORDER BY first_name ASC)
AS CNT
GROUP BY first_name

这让我明白了:
阿诺德(5)
克劳迪娅(19岁)
珍妮特(29岁)
富(21)
汤姆(4)
未分配(24)
但是,有一些结果中的零值是我非常想要的,比如:
阿诺德(5)
谢丽尔(0)
克劳迪娅(19岁)
辛迪(0)
珍妮特(29岁)
菲尔(0)
富(21)
汤姆(4)
未分配(24)
我做错什么了?太近了!谢谢!
-马特

最佳答案

在内部查询中,您过滤掉所有人,这可能会给您带来0结果。
试试这个:

SELECT first_name,
       sum(CASE WHEN job_status = 'Archived' THEN 1 ELSE 0 END) AS cnt
  FROM typesetting
  LEFT JOIN employees ON typesetting.employees_id = employees.id
  LEFT JOIN job_status ON typesetting.job_status_id = job_status.id
GROUP BY first_name
ORDER BY first_name ASC;

08-27 16:05