how to group datetime into daily/hourly

  • If table has column:

    XDateTime

    with Values:

    1/1/11 1:00

    1/1/11 1:15

    1/1/11 1:30

    1/1/11 2:00

    1/1/11 2:30

    etc..

    How do I write SQL to produce distinct hourly values per day:

    1/1/11 1:00

    1/1/11 2:00

    1/1/11 3:00

    so I can AVG the other fields that fall under each given hour's range?

  • You can group by the expression:

    DATEADD(hour, DATEDIFF(hour, 0, XDateTime), 0)

    e.g.

    SELECT

    DATEADD(hour, DATEDIFF(hour, 0, XDateTime), 0) AS XHour,

    AVG(XValue) AS XAvg

    FROM MyTable

    GROUP BY DATEADD(hour, DATEDIFF(hour, 0, XDateTime), 0)

    ORDER BY XHour

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply