Home > OS >  Oracle 18: count based on the max status on a given date, with grouped data
Oracle 18: count based on the max status on a given date, with grouped data

Time:11-21

My sample is a ticketing system, holding entries for status updates and the creation of a ticket.

Edit: sorry had the incorrect Fiddle Link

Fiddle: https://dbfiddle.uk/?rdbms=oracle_18&fiddle=a5ff4600adbab185eb14b08586f1bd29

ID TICKETID STATUS TICKET_CREATED STATUS_CHANGED
1 1 other_error 01-JAN-20 01-JAN-20 08.00.00
2 2 tech_error 01-JAN-20 01-JAN-20 09.00.00
3 3 unknown 01-JAN-20 01-JAN-20 09.10.00
4 4 unknown 01-JAN-20 01-JAN-20 09.20.00
5 4 tech_error 01-JAN-20 02-JAN-20 09.30.00
6 1 solved 01-JAN-20 02-JAN-20 10.00.00
7 2 solved 01-JAN-20 02-JAN-20 07.00.00
8 5 tech_error 02-JAN-20 02-JAN-20 08.00.00
9 6 unknown 02-JAN-20 02-JAN-20 08.30.00
10 6 solved 02-JAN-20 02-JAN-20 09.30.00
11 5 solved 02-JAN-20 03-JAN-20 08.00.00
12 4 unknown 01-JAN-20 03-JAN-20 09.00.00

I want to evaluate the data based on the ticket creation date, and obtain three things for a specific date:

  1. (done) how many tickets where totally created on the given date
  2. (done) how many tickets were created in status 'unknown' on the given date
  3. (not done) how many tickets were totally in status 'unknown' on the given date? Tricky! Because what matters is the status at the max STATUS_CHANGED below midnight of the given date.

Desired result for 01.01.2021:

TICKET_CREATED Total Created Tickets created in Unknown status Total tickets in Unknown status
01-JAN-20 4 2 2

Explanation: on 01-JAN-20, ticket 3 and 4 were in status 'unknown' at end of the day

Desired result for 02.01.2021:

TICKET_CREATED Total Created Tickets created in Unknown status Total tickets in Unknown status
02-JAN-20 2 1 1

Explanation: on 02-JAN-20, only ticket 3 was in status 'unknown' at end of the day

Current solution for part 1 2:

select ticket_created, 
count(*) as "Total Created",
sum(case when status = 'unknown' then 1 else 0 end) as "Unknown tickets created",
'?' as "Total tickets in Unknown status"
from myTable
where id in
    (select min(id) as id
    from myTable
    where ticket_created = to_date('01.01.2020', 'DD.MM.YYYY')
    group by ticketid)
group by ticket_created

Could you give me some hint on how to approach point 3?

CodePudding user response:

This is how I would achieve your aims, assuming I've understood your logic correctly:

with ticket_info as (select id,
                            ticketid,
                            status,
                            ticket_created,
                            status_changed,
                            row_number() over (partition by ticketid, trunc(status_changed) order by status_changed desc) rn_per_id_day_desc,
                            row_number() over (partition by ticketid order by status_changed) rn_per_id_asc
                     from   mytable)
select ticket_created,
       count(distinct case when trunc(ticket_created) = to_date('01/01/2020', 'dd/mm/yyyy') then ticketid end) as "Total Created",
       count(case when rn_per_id_asc = 1 and status = 'unknown' then 1 end) as "Unknown tickets created",
       count(case when rn_per_id_day_desc = 1 and status = 'unknown' then 1 end) as "Total tickets in Unknown status"
from   ticket_info
where  status_changed >= to_timestamp('01/01/2020', 'dd/mm/yyyy')
and    status_changed < to_timestamp('01/01/2020', 'dd/mm/yyyy')   interval '1' day
group by ticket_created;

db<>fiddle

You can see that first of all, I use a couple of row_number() analytic functions to provide labels for the rows - one labels the rows per id in the order they were changed (which allows us to identify the first row per id, i.e. the ticket created row), the other labels the rows per id and day in descending order (which allows us to identify the last row of the day per id).

Using that information, we can calculate all three of your cases:

  1. tickets created on a day - here I've used a count distinct, but you could change it to count(case when rn_per_id_asc = 1 then 1 end), which may well be more efficient and easier to understand.
  2. tickets created on a day as "unknown" - here I've used a conditional count: if it's the first row and it has a status unknown, then count it
  3. tickets in "unknown" status at the end of the day - here I've used another conditional count: if it's the last row of the day and the status is unknown, count it.
  • Related