简体   繁体   中英

Calculate open cases per day

I am working on queries which are based on time and I would like to get the optimal way to calculate opened cases during the day. I do have table task_interval which has 2 columns start and end .

JSON example:

[
    {
        "start" : "2019-10-15 20:41:38",
        "end" : "2019-10-16 01:44:03"
    },
    {
        "start" : "2019-10-15 20:43:52",
        "end" : "2019-10-15 22:18:54"
    },
    {
        "start" : "2019-10-16 20:21:38",
        "end" : null,
    },
    {
        "start" : "2019-10-17 01:42:35",
        "end" : null
    },
    {
        "create_time" : "2019-10-17 03:15:57",
        "end_time" : "2019-10-17 04:14:17"
    },
    {
        "start" : "2019-10-17 03:16:44",
        "end" : "2019-10-17 04:14:31"
    },
    {
        "start" : "2019-10-17 04:15:23",
        "end" : "2019-10-17 04:53:28"
    },
    {
        "start" : "2019-10-17 04:15:23",
        "end" : null,
    },
]

The result of query should return:

[
    { time: '2019-10-15', value: 1 },
    { time: '2019-10-16', value: 1 }, // Not 2! One task from 15th has ended
    { time: '2019-10-17', value: 3 }, // We take 1 continues task from 16th and add 2 from 17th which has no end in same day
]

I have written the query which will return cumulative sum of started tasks which end date is not same as started date:

SELECT 
    time,
    @running_total:=@running_total + tickets_number AS cumulative_sum
FROM
    (SELECT 
        CAST(ti.start AS DATE) start,
            COUNT(*) AS tickets_number
    FROM
        ticket_interval ti
    WHERE
        DATEDIFF(ti.start, ti.end) != 0
            OR ti.end IS NULL
    GROUP BY CAST(ti.start AS DATE)) X
        JOIN
    (SELECT @running_total:=0) total;    

If you are running MySQL 8.0, one option is to unpivot, then aggregate and perform a window sum to compute the running count:

select 
    date(dt) dt_day, 
    sum(sum(no_tasks)) over(order by date(dt)) no_tasks 
from (
    select start_dt dt, 1 no_tasks from mytable
    union all select end_dt, -1 from mytable where end_dt is not null
) t
group by date(dt)
order by dt_day

Side note: start and end are reserved words, hence not good choices for column names. I renamed those to start_dt and end_dt .


In earlier versions, we can emulate the window sum with a user variable, like so:

select 
    dt_day, 
    @no_tasks := @no_tasks + no_tasks no_tasks 
from (
    select date(dt) dt_day, sum(no_tasks) no_tasks
    from (
        select start_dt dt, 1 no_tasks from mytable
        union all select end_dt, -1 from mytable where end_dt is not null
    ) t
    group by dt_day
    order by dt_day
) t
cross join (select @no_tasks := 0) x
order by dt_day

Demo on DB Fiddle - both queries yield:

dt_day     | no_tasks
:--------- | -------:
2019-10-15 |        1
2019-10-16 |        1
2019-10-17 |        3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM