June 18, 2019 at 2:44 pm
Hi Guys,
I have a table that stores oil burned in litres per 10 second interval and I need to consolidate this to 30 minute interval.
I've tried the following statement ...
SELECT TOP (100) PERCENT DATEADD(MINUTE, ROUND(DATEDIFF(SECOND, 0, DateTime) / 10.0, 0) * 30, 0) AS Date_Time, SUM(Burner1) AS Burn1
FROM dbo.tblOilBurner
GROUP BY DATEADD(MINUTE, ROUND(DATEDIFF(SECOND, 0, DateTime) / 10.0, 0) * 30, 0)
... but get an error message.
The datediff function resulted in an overflow. The number of dateparts seperating two date/time instances is too large. Try to use datediff with a less precise datepart.
Any help with this is appreciated!
Thanks.
June 18, 2019 at 4:08 pm
That's what comes from using a formula that you found on the web without understanding exactly what it is doing. The zero that you are using is a shortcut for the date 1900-01-01. DATEDIFF can only calculate the difference in seconds between dates about 70 years apart. Currently, that means that dates compared to today's date (2019-06-18) can only go back to 1951-05-31.
You have a few options:
My choice would probably be the last one, but you haven't provided sample data, which makes it difficult to provide tested code.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
June 18, 2019 at 8:32 pm
Assuming you don't have dates before 1980, then:
SELECT TOP (100) PERCENT DATEADD(MINUTE, ROUND(DATEDIFF(SECOND, base_date, DateTime) / 10.0, 0) * 30, base_date) AS Date_Time, SUM(Burner1) AS Burn1
FROM dbo.tblOilBurner
CROSS APPLY (
SELECT CAST('19800101' AS datetime) AS base_date
) AS alias1
GROUP BY DATEADD(MINUTE, ROUND(DATEDIFF(SECOND, base_date, DateTime) / 10.0, 0) * 30, base_date)
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
June 18, 2019 at 10:07 pm
There is also a problem with your formula.
This is another reason that you are running into problems with overflow. Your conversion isn't correct.
This is part of the reason that I recommended using a tally table to create your ranges.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
June 19, 2019 at 3:45 pm
I ignored the actual calc before, but Drew is quite right, of course, that needs corrected too:
SELECT TOP (100) PERCENT
DATEADD(SECOND, DATEDIFF(SECOND, base_date, DateTime) / 1800 * 1800, base_date) AS Date_Time,
SUM(Burner1) AS Burn1
FROM ( SELECT CAST('19800401 13:41' AS datetime) AS datetime, 10 AS burner1) AS x /*dbo.tblOilBurner*/
CROSS APPLY (
SELECT CAST('19800101' AS datetime) AS base_date
) AS alias1
GROUP BY DATEADD(SECOND, DATEDIFF(SECOND, base_date, DateTime) / 1800 * 1800, base_date)
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply