Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6225725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:53:41+00:00 2026-05-24T08:53:41+00:00

I am trying to write a query to get counts of values in different

  • 0

I am trying to write a query to get counts of values in different ranges.

Let us say my table has a column called ‘Name’ and another column called ‘Value’ which is numeric.

The ‘Value’ column can take values from 1 to 100.

Currently I am writing a query like

select count(1) from table where value between 1 and 10
union all
select count(1) from table where value between 11 and 80
union all
select count(1) from table where value between 81 and 100.

The query gives me the results but seems to perform veeeeeerrrrry SLOW.

Is there a better way to do this ?

Please remember that I cannot partition the table based on the values in the ‘Value’ column as I have other columns as well.

Edit

Ok I am going to modify the above query to

select count(distinct names) from table where value between 1 and 10
union all
select count(distinct names) from table where value between 11 and 80
union all
select count(distinct names) from table where value between 81 and 100.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:53:41+00:00Added an answer on May 24, 2026 at 8:53 am

    For your first query, assuming you don’t care whether you get one row with three columns or three rows with one column, you could do something like

    SELECT SUM( CASE WHEN value BETWEEN 1 AND 10
                     THEN 1
                     ELSE 0
                 END) num_between_1_and_10,
           SUM( CASE WHEN value BETWEEN 11 AND 80
                     THEN 1
                     ELSE 0
                 END) num_between_11_and_80,
           SUM( CASE WHEN value BETWEEN 81 AND 100
                     THEN 1
                     ELSE 0
                 END) num_between_81_and_100
      FROM table_name
    

    That will let you scan the table once rather than scanning it three times.

    The second query, in general, returns a different result set. You can match your second query with a different query

    SELECT COUNT( DISTINCT( CASE WHEN value BETWEEN 1 AND 10
                                 THEN name
                                 ELSE null
                             END) ) num_distinct_between_1_and_10,
           COUNT( DISTINCT( CASE WHEN value BETWEEN 11 AND 80
                                 THEN name
                                 ELSE null
                             END) ) num_distinct_between_11_and_80,
           COUNT( DISTINCT( CASE WHEN value BETWEEN 81 AND 100
                                 THEN name
                                 ELSE null
                             END) ) num_distinct_between_81_and_100
      FROM table_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a query that extracts and transforms data from a table
I'm trying to write a query that updates rows in a table if a
I am trying to write a query that connects 3 tables. The first table
I'm trying to write a query that will return data grouped by date ranges
I'm trying to write a query which will get the maximum scores for the
I'm trying to write a small Python script that will get query results from
I'm trying to write a Javascript function to get the query string of the
I'm trying to write the following LINQ-Entities query: Get a list of Questions, that
I'm trying write a query to find records which don't have a matching record
I'm trying to write a query that will pull back the two most recent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.