How to include values that count nothing on certain day (APEX)

165 views Asked by At

I have this query:

SELECT  
COUNT(ID) AS FREQ,
TO_CHAR(TRUNC(CREATED_AT),'DD-MON') DATES
FROM TICKETS
WHERE TRUNC(CREATED_AT) > TRUNC(SYSDATE) - 32
GROUP BY TRUNC(CREATED_AT)
ORDER BY TRUNC(CREATED_AT) ASC

This counts how many tickets where created every day for the past month.
The result looks something like this:
(first 10 rows)

FREQ    DATES
3   28-DEC
4   04-JAN
8   05-JAN
1   06-JAN
4   07-JAN
5   08-JAN
2   11-JAN
6   12-JAN
3   13-JAN
8   14-JAN

The linechart that I created looks like this:

Linechart


The problem is that the days where tickets are not created (in particular the weekends) the line just goes straight to the day where there is created a ticket.

Is there a way in APEX or in my query to include the days that aren't counted?

3

There are 3 answers

2
Mahamoutou On BEST ANSWER

I added a with clause to generate last 31 days, then I left joined with your base table like below.

with last_31_days as (
select trunc(sysdate) - 32 + level dt from dual connect by trunc(sysdate) - 32 + level < trunc(sysdate)
)
SELECT  
nvl(COUNT(t.ID), 0) AS FREQ,
TO_CHAR(
        nvl(TRUNC(t.CREATED_AT), a.dt)
    ,'DD-MON') DATES
FROM last_31_days a 
    LEFT JOIN TICKETS t 
        ON TRUNC(t.CREATED_AT) = a.dt 
GROUP BY nvl(TRUNC(t.CREATED_AT), a.dt)
ORDER BY 2 ASC
;
6
Littlefoot On

As commented, using one of row generator techniques you'd create a "calendar" table and outer join it with a table that contains data you're displaying.

Something like this (see comments within code):

SQL> with yours (amount, datum) as
  2    -- your sample table
  3    (select 100, date '2021-01-01' from dual union all
  4     select 200, date '2021-01-03' from dual union all
  5     select 300, date '2021-01-07' from dual
  6    ),
  7  minimax as
  8    -- MIN and MAX date (so that they could be used in row generator --> CALENDAR CTE (below)
  9    (select min(datum) min_datum,
 10            max(datum) max_datum
 11     from yours
 12    ),
 13  calendar as
 14    -- calendar, from MIN to MAX date in YOUR table
 15    (select min_datum + level - 1 datum
 16     from minimax
 17     connect by level <= max_datum - min_datum + 1
 18    )
 19  -- final query uses outer join
 20  select c.datum,
 21         nvl(y.amount, 0) amount
 22  from calendar c left join yours y on y.datum = c.datum
 23  order by c.datum;

DATUM          AMOUNT
---------- ----------
01.01.2021        100
02.01.2021          0
03.01.2021        200
04.01.2021          0
05.01.2021          0
06.01.2021          0
07.01.2021        300

7 rows selected.

SQL>

Applied to your current query:

WITH
   minimax
   AS
      -- MIN and MAX date (so that they could be used in row generator --> CALENDAR CTE (below)
      (SELECT MIN (created_at) min_datum, MAX (created_at) max_datum
         FROM tickets),
   calendar
   AS
      -- calendar, from MIN to MAX date in YOUR table
      (    SELECT min_datum + LEVEL - 1 datum
             FROM minimax
       CONNECT BY LEVEL <= max_datum - min_datum + 1)
  -- final query uses outer join
  SELECT COUNT (t.id) AS freq, TO_CHAR (TRUNC (c.datum), 'DD-MON') dates
    FROM calendar c LEFT JOIN tickets t ON t.created_at = c.datum
   WHERE TRUNC (t.created_at) > TRUNC (SYSDATE) - 32
GROUP BY TRUNC (c.datum)
ORDER BY dates ASC
0
T. Peter On

@Littlefoot answer is perfect. but here is a cheeky way to get the similar table with format match OP output. using a simple cte for this.

   WITH cte AS (
     SELECT To_Char(Trunc(SYSDATE - ROWNUM),'DD-MON') dtcol
       FROM DUAL 
    CONNECT BY ROWNUM < 366
    )
    SELECT * FROM cte

here is db<>fiddle

and then you can simply join this cte to fill up empty date. as the origin output column date looks like a string column.
connect by is for oracle only. but I think you can still use recursive cte to get similar result in other DBMS support recursive cte.