그룹

2010-12-12 7 views
1

I 테이블 가지고그룹

spent totalsent totalused 
---------------------------- 
4  1234  123 
6  12   4 
7  45   32 

I 그룹화 필요 totalused/totalsent 0 <의 그룹 = < = 5,6 < 보냈다 소비 < = 10 등등 .

어떻게이 작업을 수행 할 수 있습니까? 당신은 단지 5 그룹을하고자하는 경우

+0

조랑말에게 감사드립니다. +1 – EvilTeach

+0

저는 속성에 대한 조건에 따라 그룹화하기를 원합니다. 여기에 (합계/합계) 0-5, 6-10, 11-15 그룹으로 지출하여 그룹화됩니다. –

답변

2

, 당신은 아마 0 이하이 코드는 상당히 공을 처리하지 않습니다 5.

with my_source_data as (
    select 4 as spent, 1234 as totalsent, 123 as totalused from dual union all 
    select 6 as spent, 12 as totalsent, 4 as totalused from dual union all 
    select 7 as spent, 45 as totalsent, 32 as totalused from dual 
) 
select 
    (spent_group -1) * 5 + 1 as lower_bound, 
    spent_group * 5 as upper_bound, totalsent, totalused 
from (
    select 
    greatest(ceil(spent/5),1) as spent_group, 
    sum(totalsent) as totalsent, sum(totalused) totalused 
    from my_source_data 
    group by greatest(ceil(spent/5),1) 
) 

에 의해 분할 된 그룹에 열 또는 아무것도 제대로 이후를 설정할 수 있습니다 하위 그룹에 모든 것을 넣고 레이블을 1-5으로 지정하지만 그 요구 사항은 다소 모호합니다.

또한 이것은 희소 한 그룹이므로 생성 할 수있는 소스 데이터가있는 경우 11-15 행만 있습니다.