简体   繁体   中英

SQL query to count how many times data in a row matches by time

I have a table which holds call data from an IVR.

Each call is on a single line and I want to be able to count how many times an activity starts but doesn't finish in a row. I have several columns but the ones I need to use are:

Time, Activity Started, Activity Finished

Time is in a standard time format, both Activity Started and Activity finished are either 1 or 0.

I want to be able to see if Activity Started = 1 and Activity Finished = 0 happens say 3 times in a row.

Time Activity Started Activity Finished
08:00:00, 0, 0,
08:01:00, 1, 1,
08:02:00, 1, 0,
08:03:00, 1, 0,
08:04:00, 1, 0,
08:05:00, 1, 0,
08:06:00, 0, 0,
08:07:00, 1, 1,
08:08:00, 0, 0,
08:09:00, 1, 1,
08:10:00, 0, 0,
08:11:00, 0, 0,
08:12:00, 1, 1,
08:13:00, 1, 0,
08:14:00, 1, 1,
08:15:00, 1, 0,

so this data set would return:

Activity, Alarm
Failed more than 3, Yes

this query returns numbers of started activities and failed ones

SELECT COUNT(started),COUNT(Failed) FROM YOUR_TABLE_NAME GROUP BY(started)

result should be for example

started | failed 
3       | 10 

and also you MUST not return TimeStamp becasue it's not aggregated field and error thrown .

To have the alarm set of when it fails more than 3 times in a row:

WITH subqry1 AS 
( 
       SELECT *, 
              CASE 
                     WHEN `activity started` = 1 
                     AND    `activity finished` = 0 THEN 1 
                     ELSE 0 
              END AS failed 
       FROM   your_table_name ), subqry2 AS 
( 
         SELECT   *, 
                  sum(failed) OVER (ORDER BY time rows BETWEEN 3 PRECEDING AND      0 following) AS failed_cnt_window3
         FROM     subqry1) 
SELECT time, 
       'Failed more than 3, Yes' AS alert 
FROM   subqry2 
WHERE  failed_cnt_window3 > 3

Using as your_table_name the one you posted above, the result of the query would be:

Time                          alert
08:05:00                      Failed more than 3, Yes

Not sure which platform you're on. This might work for you with some possible tweaks for your environment:

with data as (
    select *, row_number() over (order by "time") as rn
    from Alarms
), gaps as (
    select *, row_number() over (order by "time") as rn2
    from data
    where started = 1 and finished = 0
)
select
    'Failed more than 3' as Activity,
    case when max(grp) is not null then 'Yes' else 'No' end as Alarm
from (values (1)) dummy(n) outer apply (
    select rn - rn2 as grp
    from gaps
    group by rn - rn2
    having count(*) > 3
) g;

This should return exactly one row regardless of the answer (yes/no) and regardless of whether there's even data in the table.

https://rextester.com/OTKA25477

Hope this works for you:

cte1(Time, Activity_Started, Activity_Failed, Seqnum) as
(
select Time, Activity_Started, Activity_Failed,
    row_number() over(partition by (Activity_Started + Activity_Failed) order by Time) as Seqnum
from IVR
)
select Top 1 "Failed more than 3, Yes"
from cte1
where Activity_Started = 1 and Activity_Failed = 0 and Seqnum > 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