2013-04-26 4 views
1

은 내가, 행 이름 '하나'를 갖는 수를 계산하는 '두', 이름의 나머지를하고자 다음과 같은 값특정 값으로 그룹화 할 수 있습니까?

+------+--------+ 
| id | Name | 
+------+--------+ 
| 0 | One | 
| 1 | Two | 
| 2 | Three | 
| 3 | four | 
| 4 | One | 
| 5 | One | 
| 6 | Two | 
| 7 | five | 
+------+--------+ 

와 오라클 데이터베이스 테이블이있다. 결과 집합

+------+--------+ 
| name | count | 
+------+--------+ 
| One | 3  | 
+------+--------+ 
| Two | 2  | 
+------+--------+ 
| Rest| 3  | 
+------+--------+ 

어떤 도움을 같이해야 하는가?

답변

4
select 
    case name 
    when 'One' then 'One' 
    when 'Two' then 'Two' 
    else 'Rest' 
    end name, 
    count(*) 
from 
    my_table 
group by 
    case name 
    when 'One' then 'One' 
    when 'Two' then 'Two' 
    else 'Rest' 
    end 

방지 반복 :

select 
    name, 
    count(*) 
from (
    select 
    case name 
     when 'One' then 'One' 
     when 'Two' then 'Two' 
     else 'Rest' 
    end name 
    from 
    my_table) 
group by 
    name 
+0

고마워요. 또한 디코딩 기능을 찾고 있어요. –

+0

Decode()보다는 Case를 사용할 것입니다. ANSI와 호환되고보다 유연하며 단락 회로 평가 기능을 제공하며 SQL과 일관된 의미로 null을 처리합니다. –

+0

확인. 비교를위한 감사합니다. –

관련 문제