group by 와 case when 의 두 가지 예 를 사용 합 니 다.

1505 단어 Oacle
group by 집합 에 관 한 좋 은 글 을 보 았 습 니 다.  두 가지 예 를 정리 하 다
예 1
create table testScore    
(       
   tname varchar(30) null,    
   ttype varchar(10) null,    
   tscor int null   
);    

insert into testScore values ('  ','  ',80);   
insert into testScore values ('  ','  ',98);    
insert into testScore values ('  ','  ',65);    
insert into testScore values ('  ','  ',70);    
insert into testScore values ('  ','  ',80);    
insert into testScore values ('  ','  ',90);    


select
    tname as '  ' ,     
    max(case ttype when '  ' then tscor else 0 end) '  ',     
    max(case ttype when '  ' then tscor else 0 end) '  ',     
    max(case ttype when '  ' then tscor else 0 end) '  '     
from testScore     
group by tname

결 과 는:
성명.
국어
수학.
영어.
이사
70
80
90
장삼
80
98
65
예 2
select 
      tname as '  ',
      case
          when ttype='  ' then '  '
          else '  '
      end as '  ',
      sum(tscor) as '  '
from testscore
group by 
      tname,
      case
          when ttype='  ' then '  '
          else '  '
      end

결 과 는:
성명.
과 별
총 점
이사
문과.
160
이사
이과
80
장삼
문과.
145
장삼
이과
98

좋은 웹페이지 즐겨찾기