I have a tickets table as this:
tickets:
id integer primary key
home_org varchar
There is a homeorgs table that holds all the home organizations and includes a division code as this:
homeorgs:
home_org varchar
division_code varchar
I want to be able to show number of tickets per month per division, even if a particular division has not submitted any tickets. For the division that have no tickets, I need it to show 0 (zero).
Here is the sql:
select
count(t.id) as ticket_count,
to_number(to_char(t.submitdate,'MM'), '99') as mon,
to_number(to_char(t.submitdate,'YYYY'), '9999') as yr,
nd.division_key
from homeorgs as h
LEFT JOIN tickets as t
ON t.home_org = h.home_org
and t.submitdate >= '2018-02-01 00:00:00'
and t.submitdate <= '2018-02-28 23:59:59'
where t.home_org is not null
group by h.division_key, mon, yr
order by yr, mon, h.division_key
This sql does not bring in the homeorg rows in which no tickets have been submitted.
What am I doing wrong here?