로메오의 블로그

[SQL] Oracle SQL Query - CASE WHEN ELSE 본문

Backend/Oracle & SQL

[SQL] Oracle SQL Query - CASE WHEN ELSE

romeoh 2019. 9. 2. 23:36
반응형

[DOCKER] MAC에 DOCKER 설치하기

[ORACLE] DOCKER에 ORACLE 11G 설치하기

[POSTGRESQL] DOCKER에 POSTGRESQL 설치하기

 

SELECT 
       COMPANY
     , PRODUCT
  FROM (
    SELECT 'Google' COMPANY, 'Android' PRODUCT FROM DUAL
    UNION ALL
    SELECT 'Google' COMPANY, 'Chrome' PRODUCT FROM DUAL
    UNION ALL
    SELECT 'Apple' COMPANY, 'Macbook' PRODUCT FROM DUAL
    UNION ALL
    SELECT 'Apple' COMPANY, 'iPhone' PRODUCT FROM DUAL
    UNION ALL
    SELECT 'Sony' COMPANY, 'walkman' PRODUCT FROM DUAL
    UNION ALL
    SELECT 'Samsung' COMPANY, 'Gallexy' PRODUCT FROM DUAL
  );
COMPANY PRODUCT
------- -------
Google	Android
Google	Chrome
Apple	Macbook
Apple	iPhone
Sony	walkman
Samsung Gallexy

6 rows selected.

회사와 상품이 등록된 테이블에

CASE 문으로 국가 컬럼을 추가하겠습니다.

 

SELECT 
       COMPANY
     , PRODUCT
     , CASE
          WHEN COMPANY = 'Samsung' THEN 'KOREA'
          WHEN COMPANY = 'Sony' THEN 'JAPAN'
          ELSE 'USA'
       END AS COUNTRY
  FROM (
    SELECT 'Google' COMPANY, 'Android' PRODUCT FROM DUAL
    UNION ALL
    SELECT 'Google' COMPANY, 'Chrome' PRODUCT FROM DUAL
    UNION ALL
    SELECT 'Apple' COMPANY, 'Macbook' PRODUCT FROM DUAL
    UNION ALL
    SELECT 'Apple' COMPANY, 'iPhone' PRODUCT FROM DUAL
    UNION ALL
    SELECT 'Sony' COMPANY, 'walkman' PRODUCT FROM DUAL
    UNION ALL
    SELECT 'Samsung' COMPANY, 'Gallexy' PRODUCT FROM DUAL
  );
COMPANY PRODUCT COUNT
------- ------- -----
Google	Android USA
Google	Chrome	USA
Apple	Macbook USA
Apple	iPhone	USA
Sony	walkman JAPAN
Samsung Gallexy KOREA

6 rows selected.

회사이름에 따른 조건문으로 COUNTRY 컬럼을 생성했습니다.

반응형
Comments