I have a table that data gets stored in every 5 mins in intervals marked as 5,10,15,20...2350,2355,2400,5,10, etc. based on the current time rounded to the nearest 5 min interval. Now, with this data, I'm trying to make a query that can be ran to show the sum or average of the 5 min data for every hour interval. So, 100 would sum intervals 5-100 and 2400 would sum intervals 2305-2400.
I just figured out how to use the DataGridView controls in the 2.0 frameworks but now I'm not stuck on this query. Thanks!

Sum 5 minute data by hour
minkee.s
I figured it out after searching through the T-SQL online language reference, which is pretty nice I must say...if you know what your looking for or get lucky like I did. Anyways, here's the query:
SELECT CAST(CAST(CONVERT(INT,LEFT(RIGHT('0000'+CONVERT(NVARCHAR(4),(CASE Interval
WHEN '2400' THEN '0'
ELSE Interval END)),4),2)) AS VARCHAR(4))+'00' AS INT),
AVG(Data) AS AvgData
FROM tbTable
GROUP BY CAST(CAST(CONVERT(INT,LEFT(RIGHT('0000'+CONVERT(NVARCHAR(4),(CASE Interval
WHEN '2400' THEN '0'
ELSE Interval END)),4),2)) AS VARCHAR(4))+'00' AS INT)
ORDER BY Interval DESC
This is what I ended up with. This also adds the '00' to get my intervals back like they were originally. The CASE() was used to change the '2400' interval to '0'. Thanks for the help...btw, how do people post code so cleanly I always get the spaces between my code when I edit it after pasting...as in the above case.
mtm81
haguna99
gracias
Can you try this:
SELECT
CONVERT(INTEGER,Left(RIGHT('0000'+CONVERT(Nvarchar(4),col_minutesInterval),4),2)), SUM(col_value) AS HourSumFROM
hourMinutesValuesGROUP
BY CONVERT(INTEGER,Left(RIGHT('0000'+CONVERT(Nvarchar(4),col_minutesInterval),4),2))AND
SELECT CONVERT(INTEGER,Left(RIGHT('0000'+CONVERT(Nvarchar(4),col_minutesInterval),4),2)), AVG(col_value) AS HourAvg
FROM
hourMinutesValuesGROUP
BY CONVERT(INTEGER,Left(RIGHT('0000'+CONVERT(Nvarchar(4),col_minutesInterval),4),2))shmulik_segal
I think the range should be 0 to 2355( you cannot go to 2400, which is the same as 0.). If you do, you will have 25 hours in the result.
Steven_Cahill
SouLDragooN
Will Merydith
CBuilder
dermothogan