Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8650993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:57:15+00:00 2026-06-12T13:57:15+00:00

I have a SQL Server database with two values I’m interested in: dtime –

  • 0

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}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:57:16+00:00Added an answer on June 12, 2026 at 1:57 pm

    You can use ALL:

    IF 80 < ALL(
      SELECT temperature 
      FROM (
        SELECT temperature, dtime,
        RN = ROW_NUMBER() OVER (ORDER BY dtime DESC)
        FROM dbo.Temps 
      ) X
      WHERE RN <= 48
    )
    SELECT 'ALERT, the last 48 measurements exceeded 80 degrees!'
    ELSE 
    SELECT 'everything is okay';
    

    Fiddle: http://sqlfiddle.com/#!6/4e2c8/7/0

    Edit: As Blam has mentioned this can be simplified by using TOP

    IF 80 < ALL(
       SELECT TOP 48 temperature 
       FROM dbo.Temps
       ORDER BY dtime DESC
    )
    SELECT 'ALERT, the last 48 measurements exceeded 80 degrees!'
    ELSE 
    SELECT 'everything is okay';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have SQL Server 2008 Ent and OLTP database with two big tables. How
You have two servers when you perform a SQL Server database mirroring You have
We have two tables in a SQL Server 2005 database, A and B.There is
I have a table say T in SQL Server 2005 database and it has
I have two tables in my SQL Server database. The first is Test1 and
I have data in the following format in a sql server database table [CPOID]
I'm using SQL Server 2008 R2 and I have a database which contains two
I have a table in Microsoft SQL server Management Studio with two columns title
I have a database column in a SQL Server 2008 database that contains VARCHAR
I have a SQL Server 2008 many-to-many relationship table ( Assets ) with two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.