sql >> Database >  >> RDS >> Mysql

Berekenen van gemiddelde waarden over secties van datum/tijd

SELECT  DAY(Timestamp), HOUR(Timestamp), MINUTE(Timestamp), AVG(value)
FROM    mytable
GROUP BY
        DAY(Timestamp), HOUR(Timestamp), MINUTE(Timestamp) WITH ROLLUP

WITH ROLLUP clausule hier produceert extra rijen met gemiddelden voor elk HOUR en DAY , zoals dit:

SELECT  DAY(ts), HOUR(ts), MINUTE(ts), COUNT(*)
FROM    (
        SELECT  CAST('2009-06-02 20:00:00' AS DATETIME) AS ts
        UNION ALL
        SELECT  CAST('2009-06-02 20:30:00' AS DATETIME) AS ts
        UNION ALL
        SELECT  CAST('2009-06-02 21:30:00' AS DATETIME) AS ts
        UNION ALL
        SELECT  CAST('2009-06-03 21:30:00' AS DATETIME) AS ts
        ) q
GROUP BY
        DAY(ts), HOUR(ts), MINUTE(ts) WITH ROLLUP
2, 20, 0, 1
2, 20, 30, 1
2, 20, NULL, 2
2, 21, 30, 1
2, 21, NULL, 1
2, NULL, NULL, 3
3, 21, 30, 1
3, 21, NULL, 1
3, NULL, NULL, 1
NULL, NULL, NULL, 4

2, 20, NULL, 2 hier betekent dat COUNT(*) is 2 voor DAY = 2 , HOUR = 20 en alle minuten.



  1. Kubernetes gebruiken om PostgreSQL te implementeren

  2. Oracle Database 21c voor Linux-platforms

  3. Formatteer sysjobhistory datetime &duration Kolommen in SQL Server

  4. Inleiding tot SQL-opdrachten