I have 2 different date fields, 1 when the case was created and the other when the case completed. I am trying to count total cases and total completions in say 2023.
Whenever I run my query and set the where field, both columns return the same figure.
SELECT COUNT(*) AS cases,
COUNT(IF(YEAR(tbl_lead.CompDate) = '2023', 1, NULL)) AS Completed,
tbl_lead.LeadSource
FROM tbl_lead
WHERE YEAR(tbl_lead.CompDate) = '2023'
GROUP BY tbl_lead.LeadSource
I guess what I’m trying to do is count all records for 2023 as cases, then count how many of those have completed in 2023. Is it impossible?
It should output as:
Cases | Completed | LeadSource |
---|---|---|
1000 | 500 | |
2000 | 700 |
Whereas it currently outputs:
Cases | Completed | LeadSource |
---|---|---|
500 | 500 | |
700 | 700 |
Thank you
3
Answers
With this
where
condition, all the rows in the query are of cases completed in 2023 (i.e, match the condition in theif
), so bothcount
s will return the same result. Drop thewhere
clause and you should be OK:Your current query only refers to CompDate but in your question you refer to two different date columns. Is this what you are looking for:
I think there is a column (lets call it is_completed) that indicates whether a case is completed or not, in this case, try this: