I have a SQL Server database with two values I’m interested in:
dtime - datetime
temperature - varchar
The table is fed by an external process that takes a building’s temperature every 30 minutes. I’m interested in triggering an alert if the temperature exceeds 80 degrees for 48 periods (24 hours).
I think this needs to be an external process that scans the table and sends an alert when this condition is met. I’m struggling with writing the SQL to do this.
EDIT
The data I’m pulling in comes in on a weekly basis. During this week I need to see if at any time in a 24-hour period the temperature has exceeded 80 degrees. The air conditioning could fail at any time and span two days or more, so I need to potentially check this across multiple days. The temperature is taken every half hour, so during the week I need to check if there are 48+ instances where the temperature exceeded 80 degrees.
Sample data:
10/1/2012 12:00:00 AM | 70 | {ok}
10/1/2012 12:30:00 AM | 70 | {ok}
10/1/2012 1:00:00 AM | 70 | {ok}
10/1/2012 1:30:00 AM | 75 | {ok}
10/1/2012 2:00:00 AM | 75 | {ok}
10/1/2012 2:30:00 AM | 80 | {ok}
You can use
ALL:Fiddle: http://sqlfiddle.com/#!6/4e2c8/7/0
Edit: As Blam has mentioned this can be simplified by using
TOP