Try this too.select ServerName,ListDate,SUM(case when ListDate = EntryDate then 0 else 1 end)from (select row_number() over(partition by ServerVulnId order by ListDate) as rn,* from @t) awhere a.rn = 1group by ServerName,ListDate
Here's Sample data and Resultdeclare @t table (ServerName varchar(1), ListDate datetime, ServerVulnId int, EntryDate datetime)insert @tselect 'a' , '2010-03-01', 222, '2010-03-01'union all select 'a', '2010-03-01', 333 , '2010-03-01'union all select 'a', '2010-04-01', 222, '2010-03-01'union all select 'a', '2010-04-01', 444, '2010-03-01'union all select 'a' , '2010-04-01', 555, '2010-03-01'union all select 'a', '2010-05-01', 333, '2010-03-01'union all select 'a', '2010-05-01', 444, '2010-03-01'union all select 'a' , '2010-05-01', 666, '2010-03-01'
ResultServerName ListDate Cnt---------- ----------------------- -----------a 2010-03-01 00:00:00.000 0a 2010-04-01 00:00:00.000 2a 2010-05-01 00:00:00.000 1