If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > How to pull records in the past 30min only?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-23-12, 22:31
mlong30 mlong30 is offline
Registered User
 
Join Date: Oct 2007
Posts: 24
Question 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.
Reply With Quote
  #2 (permalink)  
Old 06-23-12, 22:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 20,000
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
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-23-12, 23:24
mlong30 mlong30 is offline
Registered User
 
Join Date: Oct 2007
Posts: 24
Post

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

the query is counting records, that don't meet the time frame.
Reply With Quote
  #4 (permalink)  
Old 06-23-12, 23:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 20,000
that would suggest that sampledate is VARCHAR instead of DATETIME

true?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-23-12, 23:49
mlong30 mlong30 is offline
Registered User
 
Join Date: Oct 2007
Posts: 24
I just checked, and its a datetime field.
Reply With Quote
  #6 (permalink)  
Old 06-24-12, 01:12
imex imex is offline
Registered User
 
Join Date: Apr 2012
Posts: 178
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On