Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
My manager wanted a report that will display the counts of transactions per hour. The date ranges from 01/01/06 to 06/15/06.I figured it out and everything but I'm just curious on how someone else would write it, in a more proper way. Any feed backs on how I should of approached it would be great, here is my code:/*******************************************************************Tables: 1)work_table - Holds all transactions for that specific date. 2)cashworks - Holds all the transactions.*******************************************************************/--TRIGGER CREATIONcreate trigger delete_trigger on work_tableafter deleteasdeclare @count int, @start datetime,@end datetimeselect @start = min(transdate),@end = max(transdate),@count = count(transdate)from deletedinsert into result_table (start_time,end_time,total)values (@start,@end,@count)--PROCEDUREcreate procedure get_transaction_per_hour( @parameter varchar(50))asinsert into work_tableselect transdatefrom cashworkswhere cast(floor(cast(transdate as float)) as datetime) = @parameterorder by transdatewhile exists (select * from work_table)begin declare @search_by datetime, @hour_ahead datetime set @search_by = (select min(transdate) from work_table) set @hour_ahead = (select dateadd(mi,60,@search_by)) delete work_table where transdate between @search_by and @hour_aheadend
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts
Posted - 2006-07-18 : 12:04:47
This will give you the total for each hour over that time range for the hours that have transactions.
select DATE_HOUR = dateadd(hh,datediff(hh,0,transdate),0), TRANSACTION_COUNT = count(*)from cashworkswhere` transdate >= '20060101' and` transdate < '20060616'group by dateadd(hh,datediff(hh,0,transdate),0)order by dateadd(hh,datediff(hh,0,transdate),0)