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

GROUP BY tidsstempel hvert 15. minut inklusive manglende poster

Hvis du virkelig vil gøre det i mysql, burde dette virke til en vis grad – alt efter hvordan du vil organisere dine 15 minutters intervaller. Jeg har givet output som time from | time to | number of reports fordi jeg synes, det er meningsløst og endda lidt vildledende at udskrive et bestemt tidsstempel og forbinde en optælling af et tidsinterval med det.

SELECT CONCAT (
        lpad(hours.a, 2, "0"),
        ":",
        lpad(minutes.a * 15, 2, "0"),
        ":00"
        ) AS 'from',
    CONCAT (
        lpad(hours.a, 2, "0"),
        ":",
        lpad((minutes.a * 15) + 14, 2, "0"),
        ":59"
        ) AS 'to',
    count(r.id)
FROM (
    SELECT 0 AS a

    UNION

    SELECT 1 AS a

    UNION

    SELECT 2 AS a

    UNION

    SELECT 3 AS a

    UNION

    SELECT 4 AS a

    UNION

    SELECT 5 AS a

    UNION

    SELECT 6 AS a

    UNION

    SELECT 7 AS a

    UNION

    SELECT 8 AS a

    UNION

    SELECT 9 AS a

    UNION

    SELECT 10 AS a

    UNION

    SELECT 11 AS a

    UNION

    SELECT 12 AS a

    UNION

    SELECT 13 AS a

    UNION

    SELECT 14 AS a

    UNION

    SELECT 15 AS a

    UNION

    SELECT 16 AS a

    UNION

    SELECT 17 AS a

    UNION

    SELECT 18 AS a

    UNION

    SELECT 19 AS a

    UNION

    SELECT 20 AS a

    UNION

    SELECT 21 AS a

    UNION

    SELECT 22 AS a

    UNION

    SELECT 23 AS a
    ) hours
INNER JOIN (
    SELECT 0 AS a

    UNION

    SELECT 1 AS a

    UNION

    SELECT 2 AS a

    UNION

    SELECT 3 AS a
    ) minutes
LEFT JOIN reports r ON hours.a = hour(r.t)
    AND minutes.a = floor(minute(r.t) / 15)
GROUP BY hours.a,
    minutes.a;



  1. Indsættelse af værdi til database ved hjælp af forberedt erklæring

  2. connection.select_value returnerer kun strenge i postgres med pg gem

  3. MySQL-forespørgsel med SUM() returnerer ikke de forventede resultater

  4. MySQL Fatal fejl:Kan ikke åbne og låse privilegietabeller:Forkert filformat 'bruger'