sql >> Database teknologi >  >> RDS >> Mysql

Beregning af gennemsnitsværdier over sektioner af dato/klokkeslæt

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

WITH ROLLUP klausul her producerer ekstra rækker med gennemsnit for hver HOUR og DAY , sådan her:

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 betyder her, at COUNT(*) er 2 for DAY = 2 , HOUR = 20 og alle minutter.



  1. Hvordan kan jeg oprette et installationsprogram til en hjemmeside. PHP mysql

  2. Hvordan håndteres InnoDB-deadlocks korrekt i Java/JDBC?

  3. R- Specialtegn indsættes ikke i mysql

  4. mysql innodb:beskriv tabel viser ikke kolonnereferencer, hvad viser dem?