Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Counts by Time Interval

$
0
0

Using SQL 2008 R2 + SP 2 x64 on Windows Server 2008 R2 x64.

I have a program that processes data from 10 PM to 8 AM each day.  I want to get a count of how many processed in each hour.

The output should be a table like this:

Here is the code I am using now:

Create Table #tmp (hr varchar(10), ctr int);

Insert into #tmp
SELECT '10',
 SUM(CASE WHEN (JobComplete BETWEEN Cast('6-14-13 22:00' As datetime) AND Cast('6-14-13 23:00' as datetime)) then 1 else 0 end) as ctr 
From Customer

Insert into #tmp
SELECT '11',
 SUM(CASE WHEN (JobComplete BETWEEN Cast('6-14-13 23:00' As datetime) AND Cast('6-15-13 00:00' as datetime)) then 1 else 0 end) as ctr 
From Customer

...(repeat code blocks above for hours 1 - 8 )...

Yes, it is not very efficient.

I would appreciate your suggestions.

Thanks

Tom


MisterT99


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>