• DaveDB (12/12/2013)


    Hello,

    I need to write a SQL query that aggregates data based on 1 hour intervals. So, basically I would need something that lists all available times: (6:30 - 7:30), (7:30 - 8:30), (8:30 - 9:30), etc. So the INTERVALSTART and END have to be on the :30, but display the full hour. I assume I have to do something in the WHERE clause as well. The column that I am using for hour intervals is date/time.

    Any help would be appreciated. Here is what I have so far...

    SELECT INTERVAL_START, INTERVAL_END, AGGREGATED_VALUE

    FROM TABLE A

    LEFT JOIN TABLE B

    ON A.logid = G.logid

    WHERE convert(varchar(15), INTERVALSTART, 101) = convert(varchar(15),getdate(),101)

    AND...... Start and End time

    AND Item_Name IN ('Conservation Only','NJ SWAT','NJ Contact Center')

    GROUP BY INTERVAL_START, INTERVAL_END, AGGREGATED_VALUE

    It sounds like you need a calendar table. Search around this site, there are several articles and LOTS of threads. You might also check out the article found by following the first link in my signature.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/