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

  • Home
  • SEARCH
  • 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 9077287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:18:24+00:00 2026-06-16T19:18:24+00:00

Possible Duplicate: In SQL, how can you group by in ranges? i have table

  • 0

Possible Duplicate:
In SQL, how can you “group by” in ranges?

i have table like that

price
id  |  prices
1   |   3910
2   |   4125
3   |   3740
4   |   2700
5   |   1600
6   |   2150
7   |   2430

my wished output is like that

1500-1999  |  2000-2499 | 2500-2999  | 3000-3499 | 3500-3999 | 4000-4499  |   allvalues
--------------------------------------------------------------------------------------
   1       |      2     |      1     |     0     |     2     |     1      |   7

expli :

  • 1500-1999 –> means i want count numbers which are between 1500 and 1999
    and so on , with others.

  • allvalues is to count all values .

    *here is the sql fiddle if someone need to test it there.

hope all is clear , thanks for any help.

  • 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-06-16T19:18:25+00:00Added an answer on June 16, 2026 at 7:18 pm

    This is a kind of pivot using ranges. The pattern is the same for a pivot query using SUM(CASE...)

    SELECT
      SUM(CASE WHEN prices BETWEEN 1500 AND 1999 THEN 1 ELSE 0 END) AS `1500-1999`,
      SUM(CASE WHEN prices BETWEEN 2000 AND 2499 THEN 1 ELSE 0 END) AS `2000-2499`,
      SUM(CASE WHEN prices BETWEEN 2500 AND 2999 THEN 1 ELSE 0 END) AS `2500-2999`,
      SUM(CASE WHEN prices BETWEEN 3000 AND 3499 THEN 1 ELSE 0 END) AS `3000-3499`,
      SUM(CASE WHEN prices BETWEEN 3500 AND 3999 THEN 1 ELSE 0 END) AS `3500-3999`,
      ...
      COUNT(*) AS `All Values`
    FROM
      prices
    

    Substitute the rest of the ranges… The CASE produces a 0 or 1 if the condition is matched and those are added up by the aggregate SUM()s.

    http://sqlfiddle.com/#!2/5803e/10

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: T-SQL Group Rows Into Columns I have a table _data that is
Possible Duplicate: SQL - How can I remove duplicate rows? I have a table
Possible Duplicate: SQL: Find the max record per group I have a table with
Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: SQL Server: Get Top 1 of Each Group I have 2 tables
Possible Duplicate: Parameterizing a SQL IN clause? Hi, I have a query looks like
Possible Duplicate: SQL Delete: can't specify target table for update in FROM clause I
Possible Duplicate: Incrementing a field in SQL using PHP I have a table (T1)
Possible Duplicate: How can an SQL query return data from multiple tables I have
Possible Duplicate: dynamic sql pivot in sql server I have a table called Col_values

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.