SQlite日夜均值

时间:2013-12-20 20:26:32

标签: sqlite

我有一个环境温度多年的数据库,我需要从每个月的06:00到20:00(白天)和晚上制作平均温度。我已经做的是

SELECT
    STRFTIME('%Y', fecha) AS year,
    STRFTIME('%m', fecha) AS month,

   AVG(T10_AVG) AS Temp_prom,
   FROM GER
GROUP BY STRFTIME('%Y', fecha), STRFTIME('%m', fecha) 

但我不知道如何包括白天和晚上的平均值。

1 个答案:

答案 0 :(得分:0)

如果要在同一月份输出记录中获得这些平均值,则无法添加另一个GROUP BY列。 您必须使用单独的子查询计算这些值:

SELECT strftime('%Y', fecha) AS year,
       strftime('%m', fecha) AS month,
       (SELECT AVG(T10_AVG)
        FROM Ger AS g2
        WHERE g2.fecha GLOB strftime('%Y-%m*', Ger.fecha)
          AND (g2.hour >= '06' AND g2.hour < '20')
       ) AS avg_day,
       (SELECT AVG(T10_AVG)
        FROM Ger AS g2
        WHERE g2.fecha GLOB strftime('%Y-%m*', Ger.fecha)
          AND (g2.hour < '06' OR g2.hour >= '20')
       ) AS avg_night,
       AVG(T10_AVG) AS Temp_prom
FROM Ger
GROUP BY year, month