SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Range change
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

JaybeeSQL
Yak Posting Veteran

77 Posts

Posted - 07/04/2012 :  07:24:49  Show Profile  Reply with Quote
Hi all,

The SQL below retrieves value counts between peak hours of 8am and 8pm, but how would I retrieve counts divided by day across 25th June, and 2nd July, and between the same times?

Cheers,

JB

select count(studyuid)
from tblAuditTrail
where command = 16385
and completioncode = 0
and DateStart > '20120630 08:00:00.000' and datestart < '20120630 20:00:00.000'
group by datestart
order by datestart

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/04/2012 :  09:57:21  Show Profile  Reply with Quote
You can change the where clause to this.
WHERE  command = 16385
       AND completioncode = 0
       AND DateStart >= '20120625'
       AND Datestart < '20120703'
       AND datepart(hour,Datestart) >= 8
       AND datepart(hour,Datstart) <= 20
Go to Top of Page

JaybeeSQL
Yak Posting Veteran

77 Posts

Posted - 07/04/2012 :  10:28:04  Show Profile  Reply with Quote
Thanks Sunita, but that doesn't divide (or rather, group) the counts for each day, it simply gives a total of the whole week.
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/04/2012 :  10:30:51  Show Profile  Reply with Quote
quote:
Originally posted by JaybeeSQL

Thanks Sunita, but that doesn't divide (or rather, group) the counts for each day, it simply gives a total of the whole week.

Didn't read your original posting carefully enough.

Change the group by clause to "GROUP BY CAST(DATESTART AS DATE)" if you are on SQL 2008 or "GROUP BY DATEADD(dd,DATEDIFF(dd,0,DATESTART),0)" if you are on SQL 2005 or earlier.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47052 Posts

Posted - 07/04/2012 :  17:26:13  Show Profile  Reply with Quote

select datestart,count(case when DATEPART(hh,datestart) BETWEEN 8 AND 20 THEN studyuid ELSE NULL END)
from tblAuditTrail
where command = 16385
and completioncode = 0
and DateStart >= '20120625' and datestart < '20120703'
group by datestart
order by datestart


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JaybeeSQL
Yak Posting Veteran

77 Posts

Posted - 07/05/2012 :  07:01:44  Show Profile  Reply with Quote
Thank you that lady!! :)
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 07/05/2012 :  07:33:44  Show Profile  Reply with Quote
You are very welcome sir :)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000