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 8049095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:41:18+00:00 2026-06-05T06:41:18+00:00

I am trying to find an elegant solution in the form of a SQL

  • 0

I am trying to find an elegant solution in the form of a SQL query for the following problem.

New records will be inserted in the Log table.
I need to detect any new records (inserted in the last hour) that I haven’t seen before and generate an alert (e.g. # of these records > 0)

ID, Url, DOB
1, site1.com/page1, "5/06/2012 20:01"
2, site2.com/page2, "5/06/2012 21:20"
3, site1.com/page1, "6/06/2012 10:05"

If “now” is 6/06/2012 10:40 – I see that there was 1 new record (id=3) inserted but I don’t want to generate an alert because we have seen this URL before (id=1).

if we have
4, site3.com/pageX, “6/06/2012 10:08”
then I want to generate an alert (return count=1) because this row was inserted in the last hour and we haven’t seen it before.

What is the best way to implement it? ideally without nested queries

  • 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-05T06:41:20+00:00Added an answer on June 5, 2026 at 6:41 am

    I think this is what you are after. This will retrieve new entries in the last hour (Where new means the same URL has not been visited unitl the last hour)

    SELECT  *
    FROM    Log
    WHERE   DOB > DATEADD(HOUR, -1, CURRENT_TIMESTAMP)
    AND     NOT EXISTS
            (   SELECT  1
                FROM    Log T1
                WHERE   T1.URL = Log.URL 
                AND     T1.DOB < DATEADD(HOUR, -1, CURRENT_TIMESTAMP)
            )
    

    Working example on SQL Fiddle

    EDIT

    Just seen a comment that you only need a count:

    SELECT  COUNT(*)
    FROM    Log
    WHERE   DOB > DATEADD(HOUR, -1, CURRENT_TIMESTAMP)
    AND     NOT EXISTS
            (   SELECT  1
                FROM    Log T1
                WHERE   T1.URL = Log.URL 
                AND     T1.DOB < DATEADD(HOUR, -1, CURRENT_TIMESTAMP)
            )
    

    EDIT 2

    I am not sure why there is the requirement of only being a single select, however, the closest I can get to a single select is this:

    SELECT  COUNT(*)
    FROM    (   SELECT  *, MIN(DOB) OVER(PARTITION BY URL) [FirstViewed]
                FROM    Log
            ) Log
    WHERE   FirstViewed >= DATEADD(HOUR, -1, CURRENT_TIMESTAMP)
    

    This will still return 2 if the same page has been visited twice in the last hour.

    http://sqlfiddle.com/#!3/5a8bc/1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve the following problem but cannot find an elegant solution.
I'm trying to find an elegant OOP solution for the following problem. Assume we
Trying to find an elegant solution for a problem in my CMS system. If
I have been trying for about an hour now to find an elegant solution
I've spent some time trying to find an elegant and cross browser solution but
I am trying to find a slightly more elegant solution to find if a
I've looked around, and I'm trying to find an elegant solution to this, and
Trying to find an elegant solution to reset a webform (or more specifically two
I'm trying to find some kind of elegant solution for fading in/out a TextView
I am trying to find simple, elegant way to get all the pertinent name,value

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.