Trying to find 3 separate counts within one column of a table, each with separate WHERE clauses. I've got 2 tables in the query, so one join. Thought this looked correct but it will not run:
SELECT stores.caption,
COUNT (
WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30'
AND tasks.deleted_at IS NULL)
AS TaskTotal ,
COUNT (
WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30'
AND tasks.deleted_at IS NULL
AND tasks.completed_at IS NULL) AS TaskOutstanding,
COUNT (
WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30'
AND tasks.deleted_at IS NULL
AND tasks.completed_at IS NULL
AND tasks.deadline < '2019-05-16') AS TaskOverdue
FROM tasks
FULL JOIN stores ON tasks.store_id=stores.id
GROUP BY stores.caption;
Best Answer
MySQL does not have
FULL
join implemented. You are likely to need only aLEFT
join (fromstores
totasks
):You can't have
WHERE
orWHEN
in aSELECT
expression like you try. There is aFILTER
feature in standard SQL but that has not been implemented either in MySQL. There are howeverCASE .. WHEN
expressions you can use just fine. Example:So the query can be written:
Now, since all 3 expression have a common condition you can simplify by moving that condition to the
ON
clause - assuming that you don't want any more columns without it.We can also add table aliases and removed the redundant
ELSE NULL
from the case expressions: