Here's one strategy:with yourTable (dt, amt) as( select dateadd(day,-number,getdate()) ,abs(convert(int, convert(varbinary, newid()))) / 10000000 --some random amount from master..spt_values where type = 'p' and number < 100)select a as amount_orders ,case r when 0 then '0-40' when 40 then '41-80' when 80 then '81-120' when 120 then '121-160' when 160 then '161-200' else '>200' end as [range]from ( select count(*) a ,case when (amt/40)*40 > 200 then 200 else (amt/40)*40 end r from yourTable group by case when (amt/40)*40 > 200 then 200 else (amt/40)*40 end ) dorder by rOUTPUT:amount_orders range------------- -------14 0-4016 41-8019 81-12026 121-16020 161-2005 >200
Be One with the OptimizerTG