7 Day Rolling Average

79 views Asked by At
insert into 
    job_data(ds,job_id,actor_id,event,language,time_spent,org)
    values
    ('2020-11-30',21,1001,'skip','English',15,'A'),
    ('2020-11-30',22,1006,'transfer','Arabic',25,'B'),
    ('2020-11-29',23,1003,'decision','Persian',20,'C'),
    ('2020-11-28',23,1005,'transfer','Persian',22,'D'),
    ('2020-11-28',25,1002,'decision','Hindi',11,'B'),
    ('2020-11-27',11,1007,'decision','French',104,'D'),
    ('2020-11-26',23,1004,'skip','Persian',56,'A'),
    ('2020-11-25',20,1003,'transfer','Italian',45,'C')
;

Above is the table from which I want to find following

Throughput: It is the no. of events happening per second. And also calculate 7 day rolling average of throughput.

I'm using MySQL version 5 and the following is my query. but I can't getting the out put.

CREATE VIEW t AS(
SELECT ds,
    COUNT(\*)/SUM(time_spent) AS t1
FROM job_data
Group By ds);

SELECT 
    ds AS 'Date',
    COUNT(\*) / SUM(time_spent) AS 'Throughput',
    (SELECT t1
    FROM t as x
    HAVING x.ds \>= t.ds - INTERVAL 6 DAY
        AND   x.ds \<= t.ds
    ) AS '7'
FROM
    job_data
GROUP BY ds
ORDER BY ds;

0

There are 0 answers