0

Possible Duplicate:
Oracle: how to “group by” over a range?

Let's say I have data that looks like this:

Item             Count
========         ========
1                123
2                1
3                47
4                117
5                18
6                466
7                202

I want to create a query that gives me this:

Count Start       Count End        Occurrences
===========       ===========      ===========
0                 100              3
101               200              2
201               300              1
301               400              0
401               500              1

Basically, I want to take a bunch of counts and group them into ranges for statistical rollups. I don't think I'm using the right keywords to find the answer to this. I am going against Oracle, though if there is an ANSI SQL answer I'd love to have it.

Community
  • 1
  • 1
RationalGeek
  • 9,425
  • 11
  • 62
  • 90

2 Answers2

1
select
    a.mini,
    a.maxi,
    count(a.item)
from
(
    select
        table.item,
        case (table.counter)
            when counter>=0 and counter<=100 then 0
            when counter>100 and counter<200 then 101
            when ....
        end as mini
        table.item,
        case (table.counter)
            when counter>=0 and counter<=100 then 100
            when counter>100 and counter<200 then 201
            when ....
        end as maxi
    from
        table
) a
group by
    a.mini,
    a.maxi
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
0

One way is using CASE statements. If you want it to be scalable, try having the range in a separate table and use JOIN to count the occurence.

user98534
  • 195
  • 3
  • 4
  • 9