Another version...declare @tbl table (item# int, Date datetime)insert @tbl select 1, '20080101'union all select 1, '20080103'union all select 2, '20080101'union all select 2, '20080102'union all select 3, '20080101'union all select 3, '20080102'union all select 3, '20080103'union all select 4, '20080101'union all select 4, '20080102'union all select 4, '20080103'union all select 4, '20080105'union all select 4, '20080106'union all select 4, '20080107'union all select 4, '20080108'union all select 5, '20071231' --watch out for thisunion all select 5, '20080101'union all select 5, '20080102'union all select 5, '20080103'union all select 5, '20080105'union all select 5, '20080107'union all select 5, '20080108'select item#, count(*) as cntfrom @tbl awhere '20080101' <= date and date < '20080201' and exists (select * from @tbl where item# = a.item# and (date = a.date - 1 or date = a.date + 1))group by item#/* Resultsitem# cnt----------- -----------2 23 34 75 5*/
Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part.