如何在每小时分组时获得3小时的滚动平均值?

时间:2015-09-02 20:07:59

标签: mysql sql

我有一张电话。它有以下列:start_time,type,duration。

我有一个查询可以获得每小时的平均通话时长:

SELECT
    CONCAT(FROM_UNIXTIME(UNIX_TIMESTAMP(t1.start_time), '%Y-%m-%d %H'), ':00') as h,
    t1.type,
    AVG(t1.duration)
FROM
    phone_calls t1

WHERE
    t1.start_time > '2015-09-02 00:00'
    AND t1.duration is not null
GROUP BY
    t1.type,
    h
ORDER BY
    h asc

我希望得到每小时过去三小时的平均值,而不是达到一小时的平均值。

我试过这个没有用的东西:

SELECT
    h,
    type,
    (

        SELECT
            dur_sum * dur_count / SUM(dur_count) rolling_avg
        FROM(
            SELECT
                CONCAT(FROM_UNIXTIME(UNIX_TIMESTAMP(t1.start_time), '%Y-%m-%d %H'), ':00') as h,
                t1.type,
                SUM(t1.duration),
                COUNT(t1.duration)
            FROM
                phone_calls t1

            WHERE
                t1.start_time > '2015-09-02 00:00'
                AND t1.duration is not null
            GROUP BY
                t1.type,
                h
            ORDER BY
                h asc
        ) as q1
        WHERE
            h BETWEEN DATE_SUB(h, INTERVAL 3 HOUR) AND h
    ) as rolling
FROM(
    SELECT
        CONCAT(FROM_UNIXTIME(UNIX_TIMESTAMP(t1.start_time), '%Y-%m-%d %H'), ':00') as h,
        t1.type,
        SUM(t1.duration),
        COUNT(t1.duration)
    FROM
        phone_calls t1

    WHERE
        t1.start_time > '2015-09-02 00:00'
        AND t1.duration is not null
    GROUP BY
        t1.type,
        h
    ORDER BY
        h asc
) as q2

该查询为我提供了所有行的滚动列的相同值。我如何更改查询以获取我正在寻找的内容?谢谢!

编辑:

这是我的第一个查询返回的内容:

h                 , type ,t_avg    
"2015-09-02 00:00", 2    ,773.5000    
"2015-09-02 00:00", 3    ,246.7966  
"2015-09-02 00:00", 1    ,377.4337  
"2015-09-02 01:00", 1    ,258.7692  
"2015-09-02 01:00", 3    ,188.9737  
"2015-09-02 02:00", 3    ,144.6471  
"2015-09-02 02:00", 1    ,254.4400  
"2015-09-02 03:00", 1    ,67.9048  
"2015-09-02 03:00", 3    ,88.9333  

这里有来自表格本身的一些样本数据:

start_time            ,type ,duration  
"2015-09-02 00:00:11" ,3    ,174  
"2015-09-02 00:00:15" ,1    ,1088  
"2015-09-02 00:00:27" ,1    ,23  
"2015-09-02 00:00:43" ,3    ,125  
"2015-09-02 00:00:52" ,1    ,31  
"2015-09-02 00:01:05" ,3    ,21  
"2015-09-02 00:01:16" ,1    ,43  
"2015-09-02 00:01:40" ,1    ,88  
"2015-09-02 00:02:17" ,1    ,117  
"2015-09-02 00:04:06" ,1    ,22  
"2015-09-02 00:04:13" ,1    ,46  
"2015-09-02 00:04:15" ,1    ,116  
"2015-09-02 00:04:29" ,3    ,57  
"2015-09-02 00:04:44" ,3    ,728  
"2015-09-02 00:04:57" ,3    ,132   

这就是我想要的数据:

h                 , type ,rolling_avg    
"2015-09-02 00:00", 2    ,<average duration for past three hours for type 2>     
"2015-09-02 00:00", 3    ,<average duration for past three hours for type 3>  
"2015-09-02 00:00", 1    ,<average duration for past three hours for type 1>  
"2015-09-02 01:00", 1    ,<average duration for past three hours for type 1>  
"2015-09-02 01:00", 3    ,<average duration for past three hours for type 3>  
"2015-09-02 02:00", 3    ,<average duration for past three hours for type 3>  
"2015-09-02 02:00", 1    ,<average duration for past three hours for type 1>  
"2015-09-02 03:00", 1    ,<average duration for past three hours for type 1>  
"2015-09-02 03:00", 3    ,<average duration for past three hours for type 3>  

2 个答案:

答案 0 :(得分:0)

我会给你简单的方法。因为没有架构。

如果已经按小时计算,则只需添加count字段

SELECT
    CONCAT(FROM_UNIXTIME(UNIX_TIMESTAMP(t1.start_time), '%Y-%m-%d %H'), ':00') as h,
    t1.type,
    AVG(t1.duration) t_avg,
    COUNT(t1.duration) t_count

添加限额和订单
ORDER BY h
LIMIT 3

然后通过该查询

SELECT SUM( t_avg * t_count ) / SUM( t_count )
FROM <previous query>

答案 1 :(得分:0)

连接你的桌子3次
[expectation fulfill];

第一个是:

Union all

,第二个是date_add的间隔--1小时 第三个是date_add的间隔--2小时

之后运行外部查询:(select CONCAT(FROM_UNIXTIME(UNIX_TIMESTAMP(t1.start_time), '%Y-%m-%d %H'), ':00') as h, t1.type,duration from phone_calls t1)