I have a simple table structure where each item row has a status. What I’m looking is to query for each status and count how many items use that state. It’s possible that not all statuses will be used so I need to account for nulls and zero counts.
My table structures is as follows:
Table: Items
Id, State_id, Text, Status
Table: States
State_id, Name
This is what I have:
SELECT statealias1_.State_id as y0_,
count(this_.Id) as y1_
FROM Items this_
right join States statealias1_
on this_.State_id = statealias1_.State_id
WHERE (not (statealias1_.State_id in (5, 6, 7, 8))
or statealias1_.State_id is null)
and (this_.Status = 'InProgress'
or this_.Status is null)
GROUP BY statealias1_.State_id;
This query works when all states included (there are 8 states and I’m excluding the second half). I’m not sure why this isn’t returning me all states with count regardless of nulls as I appear to be including nulls.
1 Answer