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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Calculating Hourly Totals....
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mitin
Yak Posting Veteran

81 Posts

Posted - 01/16/2013 :  09:56:21  Show Profile  Reply with Quote
Hi, I have the below query:


SELECT distinct servername, eventid,
       DATEPART(hour, slastoccurrence)  AS HourOfDay,
       DATEADD(dd,DATEDIFF(dd,0,slastoccurrence),0) AS Date,
       SUM(tally)                        AS NumberOfEvents,
       SUM(COUNT(*)) OVER(PARTITION BY DATEADD(dd,DATEDIFF(dd,0,slastoccurrence),0)) as DayTotal,
       SUM(COUNT(*)) OVER(PARTITION BY servername, DATEADD(dd,DATEDIFF(dd,0,slastoccurrence),0)) as DayTotalByServer

FROM   datatable
WHERE
sfirstoccurrence >= '2012-11-21' and sfirstoccurrence < '2012-11-22' and
slastoccurrence >= '2012-11-21' 
AND slastoccurrence < '2012-11-22'
GROUP BY
       servername,
       eventid,
       DATEPART(hour, slastoccurrence),
       DATEADD(dd,DATEDIFF(dd,0,slastoccurrence),0)
order by hourofday


This query gives an individual total for each "eventID" within each hour, but I would like to simplify it to give a total "NumberOfEvents" within each hour....I'm no longer interested in the total for each individual event id....

Not sure how to do this, can anyone help please?

Thanks

webfred
Flowing Fount of Yak Knowledge

Germany
8766 Posts

Posted - 01/16/2013 :  10:05:37  Show Profile  Visit webfred's Homepage  Reply with Quote
Try removing eventid from select column list AND from group by clause...


Too old to Rock'n'Roll too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/16/2013 :  23:43:18  Show Profile  Reply with Quote
the distinct in select doesnt make any sense as you're already grouping so please remove it

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

Go to Top of Page

mitin
Yak Posting Veteran

81 Posts

Posted - 01/17/2013 :  04:34:33  Show Profile  Reply with Quote
Thanks guys I removed eventid from the select and group by clause and that worked :)

also, I removed the distinct, thanks.

A step further... if I wanted to get a total for every 5 minutes rather than an hour, anyone know how I would do this?

Thanks
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3742 Posts

Posted - 01/17/2013 :  04:39:17  Show Profile  Reply with Quote
Add a column in the select list and add also to the grouping:
DATEADD(mi,DATEDIFF(mi,0,slastoccurrence)/5*5,0)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/17/2013 :  04:42:28  Show Profile  Reply with Quote
see generic function i created for similar scenarios

http://visakhm.blogspot.in/2010/02/aggregating-data-over-time-slots.html

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

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.27 seconds. Powered By: Snitz Forums 2000