Skip to content
Advertisement

Oracle SQL – return the date record when there is no count result

I have the tables below and I need my query to bring me the amount of operations grouped by date. For the dates on which there will be no operations, I need to return the date anyway with the zero count. Kind like that:

— QUERY I TRIED

— TABLES

Advertisement

Answer

I guess that it is a calendar you need, i.e. a table which contains all dates involved. Otherwise, how can you display something that doesn’t exist?

This is what you currently have (I’m using only the operation table; add another one yourself):

As there are no rows that belong to 02/06/2019, query can’t return anything (you already know that).


Therefore, add a calendar. If you already have that table, fine – use it. If not, create one. It is a hierarchical query which adds level to a certain date. I’m using 01/06/2019 as the starting point, creating 5 days (note the connect by clause).


Probably a better option is to dynamically create a calendar so that it doesn’t depend on any hardcoded values, but uses the min(date_operation) to max(date_operation) time span. Here we go:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement