Re: SQL group by a range?

From: Ranga Chakravarthi <ranga_at_removethis.cfl.rr.com>
Date: Thu, 28 Feb 2002 04:21:45 GMT
Message-ID: <tfif8.4225$1p6.988774_at_typhoon.tampabay.rr.com>


You can use CASE like this:

SELECT SUM(CASE WHEN grade BETWEEN 90 AND 100 THEN 1 ELSE 0 END) as "90 to 100",

           SUM(CASE WHEN grade BETWEEN 80 AND 89 THEN 1 ELSE 0 END) as "80 to 89",
etc.
FROM <table>

"Neothinker" <neo_thinker_at_hotmail.com> wrote in message news:d351ed4f.0202261405.7a3ef902_at_posting.google.com...
> I am trying to create a query that will tally the number of students
> with a grade from 90 to 100, 80 to 89, 70 to 79 ,... etc. So if I have
> a table that just had a "STUDENT" column and a "GRADE" column, what
> generic SQL could I use? As far as I know, the GROUP BY only accepts a
> column name, not an expression.
>
Received on Thu Feb 28 2002 - 05:21:45 CET

Original text of this message