how can i count values between min and max out from a table?

Posted on 2009-02-17
Last Modified: 2013-12-17
hello experts,
i have the following table:
id : integer
date: timestamp
weight: integer.
paid : boolean.

i need a query that will give me an answer as following :
1-19 | 20-29| 30-39|
25 | 57 | 74|

here i can read that records containing between 1-19 appear 25 times in my table,
the next array between 20 and 29 appear 57 times....etc.

what would be the query?
thank in advance
Question by:toshi_

    Author Comment

    my starting point is as follow,
    i need an answer for diffrerent values :

    SELECT count(*)
    FROM trash_prod
    WHERE weight BETWEEN 2  AND 19;

    LVL 10

    Accepted Solution


      case when weight between 0 and 19 then '0-19' 
      case when weight between 20 and 29 then '20-29' 
      case when weight between 30 and 39 then '30-39' 
      else 'other' 
      end as weightRange, count(*)  
    FROM trash_prod
    GROUP BY 1

    Open in new window


    Author Comment

    if i try it, i receive the following error

    ERROR:  syntax error at or near "case"
    LINE 7:   case when weight between 21 and 40 then '20-29'

    ********** Error **********

    ERROR: syntax error at or near "case"
    SQL state: 42601
    Character: 147


    Author Comment

    i found the error , i only need one case ;o)
    thanks a lot
    LVL 10

    Expert Comment

    Oh sorry. Copy and paste error. Yes only one CASE.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now