Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2007
    Posts
    25

    Question Unanswered: How to pull records in the past 30min only?

    Hi,

    I need to show the count that occurred during the previous 30min. I've been told the following query doesn't work. Any suggestions?

    SELECT COUNT(*) FROM sampledata
    WHERE (id = 254 AND sub_id = 731) AND (sampledate >= DATEADD(mi, -30, GETDATE()))

    Thanks.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    unfortunately, i am not familiar with the "doesn't work" error message

    you will need to be more specific, because the query looks okay to me

    there are several unnecessary parentheses, but they will work fine
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Oct 2007
    Posts
    25

    Post

    sampledate = '6/23/2012 6:26:38 AM'

    the query is counting records, that don't meet the time frame.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    that would suggest that sampledate is VARCHAR instead of DATETIME

    true?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Oct 2007
    Posts
    25
    I just checked, and its a datetime field.

  6. #6
    Join Date
    Apr 2012
    Posts
    213
    Hi,

    Try something like that:

    Code:
    SELECT COUNT(*) FROM sampledata
    WHERE (id = 254 AND sub_id = 731) AND
          (DATEDIFF(mi, sampledate, GETDATE()) between 0 and 30)
    Hope it's useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •