try to properly intent query while posting for better readabilityselect sum(num_quotes) as total_quotes, sum(num_errors) as total_errors from ( select hour, case when ptime <= 1000 then 1 when ptime <= 3000 then 3 when ptime <= 5000 then 5 when ptime <= 10000 then 10 when ptime <= 15000 then 15 when ptime <= 20000 then 20 when ptime <= 25000 then 25 when ptime <=30000 then 30 when ptime <=40000 then 40 else 1000 end as under_seconds, count(*) as num_quotes, sum(error) as num_errors from ( select pos_id, datediff(ms,min(entry_date),max(entry_date)) as ptime, min(entry_date) as start, max(entry_date) as finish, max(case when entry_data like '%<MsgStatusCd>Error</MsgStatusCd>%' then 1 else 0 end) as error,max(datepart(hour,entry_date)) as hour from pos_log (nolock) where entry_type in (1,3) and entry_date between '03/31/09 00:00:00' and '03/31/09 23:59:59' group by pos_id ) as a group by hour, case when ptime <= 1000 then 1 when ptime <= 3000 then 3 when ptime <= 5000 then 5 when ptime <= 10000 then 10 when ptime <= 15000 then 15 when ptime <= 20000 then 20 when ptime <= 25000 then 25 when ptime <=30000 then 30 when ptime <=40000 then 40 else 1000 end )r