Jump to content

Create a range and add count/sum of values within each range

Recommended Posts

Hi all,

Newbie to the forum and the world of SQL, hence apologies for my naivety.

From the attached table, I'm trying to create a range (as an output) and create a summary as shown in the attached in yellow. The reason being, the size of raw data (+2m rows). Trying to do this ins Microsoft SQL Management Studio.

Any help would be greatly appreciated.

Thank you.



Link to post
Share on other sites
  • 2 weeks later...

I've written the query plz refer it.

select t.order_count as [score range], count(*) as [number of occurrences]
from (
  select case  
    when order_amount between 50 and 100 then ' 50 - 100 '
    when order_amount between 101 and 150 then ' 101 - 150 '
    when order_amount between 151 and 200 then ' 151 - 200 '
    when order_amount between 201 and 250 then ' 201 - 250 '
    when order_amount between 251 and 300 then ' 251 - 300 '
    else ' 300+ ' end as order_count
  from order) t
group by t.order_count order by count(*) .

I hope you get the output you want.


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...