database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
ㆍDB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
DB2 Q&A 3404 게시물 읽기
No. 3404
반복되는 쿼리 . 줄이는 방법 없을까요?
작성자
질문자
작성일
2014-11-26 20:55
조회수
4,592

       t.apple1,
       t.apple2,
       t.apple3,
       t.apple4,
       t.apple5,
       t.apple6,
       t.apple7,
       t.apple8,
       t.banana1,
       t.banana2,
       t.banana3,
       t.banana4,
       t.banana5,
       t.banana6,
       t.banana7,
       t.banana8
from(
select( select fruitnm frmo TB_CODE c where c.fruitcd=a.fruitcd) as fruitcd,
        sum(case when a.fruitkind = 'apple' and a.score='1' then 1 else 0 end) as apple1,
sum(case when a.fruitkind = 'apple' and a.score='2' then 1 else 0 end) as apple2,
        sum(case when a.fruitkind = 'apple' and a.score='3' then 1 else 0 end) as apple3,
sum(case when a.fruitkind = 'apple' and a.score='4' then 1 else 0 end) as apple4,
sum(case when a.fruitkind = 'apple' and a.score='4' then 1 else 0 end) as apple5
sum(case when a.fruitkind = 'apple' and a.score='6' then 1 else 0 end) as apple6,
        sum(case when a.fruitkind = 'apple' and a.score='7' then 1 else 0 end) as apple7,
sum(case when a.fruitkind = 'apple' and a.score='8' then 1 else 0 end) as apple8,
        sum(case when a.fruitkind = 'banana' and a.score='1' then 1 else 0 end) as banana1,
sum(case when a.fruitkind = 'banana' and a.score='2' then 1 else 0 end) as banana2,
        sum(case when a.fruitkind = 'banana' and a.score='3' then 1 else 0 end) as banana3,
sum(case when a.fruitkind = 'banana' and a.score='4' then 1 else 0 end) as banana4,
        sum(case when a.fruitkind = 'banana' and a.score='4' then 1 else 0 end) as banana5,
sum(case when a.fruitkind = 'banana' and a.score='6' then 1 else 0 end) as banana6,
        sum(case when a.fruitkind = 'banana' and a.score='7' then 1 else 0 end) as banana7,
sum(case when a.fruitkind = 'banana' and a.score='8' then 1 else 0 end) as banana8
from TB_FRUIT a
group by a.fruitcd
)t
order by t.fruitcd


 

사과와 바나나 등 여러 과일들의 점수를 통계하고자 짠 쿼리인데요.(과일이 여러종류가 있는데 이중에 사과,바나나만 써놨습니다.)

점수에 관한 정보는 TB_FRUIT에 score컬럼에 다 들어있습니다.

(TB_CODE는 그냥 과일에 대한 영문코드와 한글말코드가 함께 나와있는 테이블일 뿐이고,큰 의미는 없습니다.)

이걸 간단하게 줄일수있는 방법 없을까요?

 

[Top]
No.
제목
작성자
작성일
조회
3407DB2 오라클호환모드 문의입니다. [1]
정융기
2015-01-19
4978
3406DB2 데이타베이스 복원 방법이 있을까요? [1]
김형래
2014-12-18
4058
3405group by 한 후 group by 된 컬럼 이외의 컬럼을 조건으로 사용하는 방법 [2]
디비투
2014-12-12
4463
3404반복되는 쿼리 . 줄이는 방법 없을까요?
질문자
2014-11-26
4592
3401[POSTGRESQL->DB2 마이그레이션] abstime 변환 법 알려주세요ㅜ.ㅜ [1]
dbghktjr
2014-11-14
5875
3400SQLCODE: -873, SQLSTATE: 53090
오니니
2014-09-18
4009
3399db2move Export시 권한 문제 질문
김홍원
2014-08-27
4156
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2023 DSN, All rights reserved.
작업시간: 0.050초, 이곳 서비스는
	PostgreSQL v16.1로 자료를 관리합니다