GROUP BY日期时间与时移

时间:2012-07-24 15:48:41

标签: mysql datetime group-by

以下声明给出了每天的平均风速和总产量,按地点ID和工厂编号分组。

SELECT STE_ID, PNT_NO, T_SMP, AVG(AVR_WS), SUM(AVR_PW) FROM argosrecord GROUP BY STE_ID , PNT_NO , YEAR(T_SMP) , MONTH(T_SMP) , DAY(T_SMP)

我想调整此查询来计算从凌晨2点到凌晨2点而不是午夜到午夜的平均值和总和。

1 个答案:

答案 0 :(得分:1)

select STE_ID,
    PNT_NO,
    YEAR(dateadd(hour, -2, T_SMP)) as Year,
    MONTH(dateadd(hour, -2, T_SMP)) as Month,
    DAY(dateadd(hour, -2, T_SMP)) as Day,
    AVG(AVR_WS) as AVG_AVR_WS,
    SUM(AVR_PW) as SUM_AVR_PW
from argosrecord
group by STE_ID,
    PNT_NO,
    YEAR(dateadd(hour, -2, T_SMP)),
    MONTH(dateadd(hour, -2, T_SMP)),
    DAY(dateadd(hour, -2, T_SMP))

对于那些使用MySQL的人:

select 
    STE_ID,
    PNT_NO,
    T_SMP,
    avg(AVR_WS),
    sum(AVR_PW)
FROM
    argosrecord
GROUP BY 
    STE_ID , 
    PNT_NO , 
    YEAR(DATE_ADD(T_SMP, INTERVAL -2 HOUR)) , 
    MONTH(DATE_ADD(T_SMP, INTERVAL -2 HOUR)) , 
    DAY(DATE_ADD(T_SMP, INTERVAL -2 HOUR))