The best way to do this is with a tally table. A tally table is a table of all integers from 1 to whatever(I use 10,000). I keep my tally table in a database called ToolBox and call the table numbers. It has one column "n".drop table #cigsgocreate table #cigs ( labelid varchar(50), date_scan datetime )insert into #cigsselect 'Y45768', '07/23/2003 12:34:40' union select 'Y45769','07/23/2003 12:45:32'union select 'Y45770','07/23/2003 10:09:30'union select 'Y45771','07/23/2003 10:34:21'godeclare @start datetime, @end datetimeselect @start = '07/23/2003', @end = '07/24/2003'select dateadd(hh,n-2,@start) as 'Start of time period', count(distinct labelid) as 'Count of labelid'from toolbox.dbo.numbers n left join #cigs c on c.date_scan >= dateadd(hh,n-2,@start) and c.date_scan < dateadd(hh,n,@start)where n <= datediff(hh,@start,@end) and n % 2 = 0group by dateadd(hh,n-2,@start)order by dateadd(hh,n-2,@start)go
Jay White{0}