I’m looking to write a query that grabs a count of a field and groups it together, but I want it to return ALL values even if the count is 0.
Here’s what I have:
SELECT COUNT(*)
,PMI
FROM Items AS AA
JOIN Headers AS BB
ON AA.DocID = BB.ID
WHERE (DocDate = CAST(CONVERT(varchar(8), GETDATE() - 1, 1) AS datetime))
AND PMI != ''
GROUP BY PMI
Here’s what I want to see:
COUNT PMI
1 900330
5 900702
0 900550
0 900331
But here’s what I’m getting:
COUNT PMI
1 900330
5 900702
I’ve never had to write a query to accomplish this before so I apologize if this is trivial.
EDIT: I realized that these tables do not contain all of the items I am looking for. I have a lookup table called ‘lookup’ which contains all of these items I wish to query for counts.
EDIT 2: Thanks everyone. Made it work by creating a view and using the lookup table and an outer join. Did the trick wonderfully.
You are using an
INNER JOINwhich returns no row if the match fials. You should use anOUTER JOINinstead.Assuming that
PMIis onItems: