[Day7] Oracle 7 [10/5]


1.  TO_CHAR( ,  'format') ํ•จ์ˆ˜์—์„œ 'format'์— ์‚ฌ์šฉ๋˜๋Š” ๊ธฐํ˜ธ๋ฅผ ์ ์œผ์„ธ์š”.
  ใ„ฑ. ๋…„๋„ : Y, YY, YYY, [YYYY],  IY, IYY, IYYY, IYYYY, YEAR, SYEAR, RR, RRRR
  ใ„ด. ์›” : MM, MONTH, MON
  ใ„ท. ์›”์˜ ์ผ :  DD
      ์ฃผ์˜ ์ผ :  D
      ๋…„์˜ ์ผ :  DDD
  ใ„น. ์š”์ผ :  DY,   DAY
  ใ…. ์›”์˜ ์ฃผ์ฐจ :   W
      ๋…„์˜ ์ฃผ์ฐจ :   WW, IW
  ใ…‚. ์‹œ๊ฐ„/24์‹œ๊ฐ„ : HH, HH24, HH12
  ใ……. ๋ถ„ : MI 
  ใ…‡. ์ดˆ : SS
  ใ…ˆ. ์ž์ •์—์„œ ์ง€๋‚œ ์ดˆ :  SSSSS
  ใ…Š. ์˜ค์ „/์˜คํ›„ :  AM,   PM
  
  TS  ์‹œ๊ฐ„ ์˜คํ›„ 3:12:33
  DS  ๋‚ ์งœ 
  DL  ๋‚ ์งœ 
  
2. ๋ณธ์ธ์˜ ์ƒ์ผ๋กœ๋ถ€ํ„ฐ ์˜ค๋Š˜๊นŒ์ง€ ์‚ด์•„์˜จ ์ผ์ˆ˜, ๊ฐœ์›”์ˆ˜, ๋…„์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์„ธ์š”..

  SELECT  TO_DATE(  '1993.12.10'  )
         , SYSDATE         
         , CEIL( ABS( TO_DATE(  '1993.12.10'  ) - SYSDATE ) ) ์ผ์ˆ˜
         , MONTHS_BETWEEN( SYSDATE,  TO_DATE(  '1993.12.10'  )  )  ๊ฐœ์›”์ˆ˜
         , MONTHS_BETWEEN( SYSDATE,  TO_DATE(  '1993.12.10'  )  ) / 12 )  ๋…„์ˆ˜
  FROM dual;


   ์ ˆ์ƒ  CEIL
   ๋ฐ˜์˜ฌ๋ฆผ  ROUND
   ์ ˆ์‚ญ   FLOOR, TRUNC


3. IW์™€ WW ์˜ ์ฐจ์ด์ . *****

-- 'WW' : 1์ผ-7์ผ์„ ๊ธฐ์ค€์œผ๋กœ ์ฃผ์ฐจ ํ‘œ์‹œ
-- 'IW' : ์ผ์š”์ผ์„ ๊ธฐ์ ์œผ๋กœ ์ฃผ์ฐจ ํ‘œ์‹œ

-- WW : ~> ์š”์ผ์— ๊ด€๊ณ„ ์—†์ด 7์ผ์„ ๊ธฐ์ค€์œผ๋กœ ์ฃผ์ฐจ๋ฅผ ๊ตฌ๋ถ„
-- 01~07์ผ ๊นŒ์ง€ 1์ฃผ์ฐจ
-- 08~14์ผ ๊นŒ์ง€ 2์ฃผ์ฐจ
-- 15~21์ผ ๊นŒ์ง€ 3์ฃผ์ฐจ
-- 
--
--IW : ~> ์š”์ผ(์›”ํ™”์ˆ˜๋ชฉ๊ธˆํ† ์ผ ์ˆœ)์„ ๊ธฐ์ค€์œผ๋กœ ์ฃผ์ฐจ๋ฅผ ๊ตฌ๋ถ„                 ISO ํ‘œ์ค€
--์ผ~์›”์š”์ผ ๋„˜์–ด๊ฐ€๋ฉด์„œ ์ฃผ์ฐจ๊ฐ€ ๋ฐ”๋€๋‹ค.
'2022.01.01'         IW

SELECT   TO_CHAR(  TO_DATE('2022.01.01') , 'IW' )   -- ๋…„์ค‘์˜ 52์ฃผ์ฐจ   ํ† 
         , TO_CHAR(  TO_DATE('2022.01.02') , 'IW' )   -- ๋…„์ค‘์˜ 52์ฃผ์ฐจ ์ผ
--        , TO_CHAR(  TO_DATE('2022.01.01') , 'WW' )  -- ๋…„์ค‘์˜ 1์ฃผ์ฐจ
--        , TO_CHAR(  TO_DATE('2022.01.07') , 'WW' )  -- ๋…„์ค‘์˜ 1์ฃผ์ฐจ
--        , TO_CHAR(  TO_DATE('2022.01.08') , 'WW' )  -- ๋…„์ค‘์˜ 2์ฃผ์ฐจ
FROM dual;


4. 
  4-1. ์ด๋ฒˆ ๋‹ฌ์ด ๋ช‡ ์ผ๊นŒ์ง€ ์žˆ๋Š” ํ™•์ธ.

  SELECT  SYSDATE
     , LAST_DAY( SYSDATE )
     , TO_CHAR( LAST_DAY( SYSDATE ),  'DD'   )   -- ๋ฌธ์ž
     , EXTRACT( DAY   FROM LAST_DAY( SYSDATE ) )  -- ์ˆซ์ž
  FROM dual;


  4-2. ์˜ค๋Š˜์ด ๋…„์ค‘ ๋ช‡ ์งธ ์ฃผ, ์›”์ค‘ ๋ช‡ ์งธ์ฃผ์ธ์ง€ ํ™•์ธ. 

 SELECT  SYSDATE
     , TO_CHAR( SYSDATE, 'WW' ) -- ๋…„์ค‘ ๋ช‡ ์งธ์ฃผ
     , TO_CHAR( SYSDATE, 'IW' )  --    "
     , TO_CHAR( SYSDATE, 'W' )    -- ๊ทธ ๋‹ฌ์˜ ๋ช‡ ์งธ์ฃผ 
  FROM dual;


5. emp ์—์„œ  pay ๋ฅผ NVL(), NVL2(), COALESCE()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถœ๋ ฅํ•˜์„ธ์š”.

SELECT sal + NVL( comm, 0 ) pay
  , sal + NVL2( comm, comm, 0 ) pay
  , COALESCE( sal + comm , sal , 0 ) pay
  , sal + COALESCE( comm ,   0 ) pay
FROM emp;


5-2. empํ…Œ์ด๋ธ”์—์„œ mgr์ด null ์ธ ๊ฒฝ์šฐ -1 ๋กœ ์ถœ๋ ฅํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ
mgr ์ปฌ๋Ÿผ : ์ง์†์ƒ์‚ฌ์˜ ์‚ฌ์›๋ฒˆํ˜ธ(empno)
mgr ์ด ๋„(null) ์ด๋ผ๋ง์€ ์ง์†์ƒ์‚ฌ X ,  CEO 

      ใ„ฑ. nvl()
      ใ„ด. nvl2()
      ใ„ท. COALESCE()

      SELECT  NVL( mgr, -1)
      , NVL2( mgr, mgr , -1)
      , COALESCE( mgr , -1 )
      FROM emp
      -- WHERE mgr IS [NOT] NULL;



6. insa ์—์„œ  ์ด๋ฆ„,์ฃผ๋ฏผ๋ฒˆํ˜ธ, ์„ฑ๋ณ„( ๋‚จ์ž/์—ฌ์ž ), ์„ฑ๋ณ„( ๋‚จ์ž/์—ฌ์ž ) ์ถœ๋ ฅ ์ฟผ๋ฆฌ ์ž‘์„ฑ-
    ใ„ฑ. DECODE()

    ใ„ด. CASE ํ•จ์ˆ˜

 SELECT name, ssn
     , DECODE(   MOD( SUBSTR( ssn, -7, 1) , 2 ) , 1, '๋‚จ์ž', '์—ฌ์ž' ) gender
     , CASE  -- DECODE ํ•จ์ˆ˜์˜ ํ™•์žฅ. = + if~
     1) CASE ๊ตฌ๋ฌธ ํ˜•ํƒœ 1
         WHEN MOD( SUBSTR( ssn, -7, 1) , 2 ) = 1 THEN '๋‚จ์ž'
--       WHEN ์กฐ๊ฑด์‹ THEN
--       WHEN THEN    
--        :
         ELSE '์—ฌ์ž'                   
       END gender
       
     2) CASE ๊ตฌ๋ฌธ ํ˜•ํƒœ 2
     , CASE MOD( SUBSTR( ssn, -7, 1) , 2 )
          WHEN  1 THEN '๋‚จ์ž'
          ELSE     '์—ฌ์ž'                 
        END gender
FROM insa;



7. emp ์—์„œ ํ‰๊ท PAY ๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ํฐ ์‚ฌ์›๋“ค๋งŒ์˜ ๊ธ‰์—ฌํ•ฉ์„ ์ถœ๋ ฅ.
  ( DECODE, CASE ์‚ฌ์šฉํ•ด์„œ ํ’€์ด ) 

  WITH 
   temp AS (
          SELECT  ename, sal+NVL(comm,0) pay
                , (  
                     SELECT AVG( sal+NVL(comm,0) )
                     FROM emp
                   )  avg_pay
          FROM emp
  )
  SELECT 
    --t.*
    SUM( DECODE(  SIGN( pay - avg_pay ), -1, null, t.pay ) )
    ,SUM( CASE
                 WHEN pay >= avg_pay  THEN   pay
                 -- ELSE null
          END
      )
    , SUM(CASE SIGN( pay - avg_pay )
          WHEN -1 THEN null
          ELSE         pay
       END
       )
  FROM temp t;


  
 --

 SELECT   SUM(   sal + NVL( comm, 0 ) ) tot_pay
         , COUNT( *) 
         , SUM(   sal + NVL( comm, 0 ) ) /  COUNT( *)  avg_pay
         , AVG(   sal + NVL( comm, 0 )  ) avg_pay 
 FROM emp;


-- 2260.416666666666666666666666666666666667

 SELECT  SUM(   sal + NVL( comm, 0 ) )
 FROM emp  -- ์ธ๋ผ์ธ๋ทฐ
 WHERE sal + NVL( comm, 0 )  >= (SELECT AVG(   sal + NVL( comm, 0 )  ) FROM emp);   
 -- ์ค‘์ฒฉ ์„œ๋ธŒ์ฟผ๋ฆฌ
  -- ORA-00934: group function is not allowed here
 --  WHERE sal + NVL( comm, 0 )  >= AVG(   sal + NVL( comm, 0 )  ); 
 WHERE sal + NVL( comm, 0 )  >= 2260.416666666666666666666666666666666667;



   
   (๋ฌธ์ œ) insa ํ…Œ์ด๋ธ”์—์„œ ๋‚จ์ž ์‚ฌ์›์ˆ˜, ์—ฌ์ž ์‚ฌ์›์ˆ˜ ์ถœ๋ ฅ(์กฐํšŒ)

 SELECT '์ด์‚ฌ์›์ˆ˜' , COUNT(*)
 FROM insa 
 UNION ALL
 SELECT '๋‚จ์ž์‚ฌ์›์ˆ˜' , COUNT(*)
 FROM insa
 WHERE MOD( SUBSTR( ssn, -7, 1) , 2) = 1
 UNION ALL
 SELECT '์—ฌ์ž์‚ฌ์›์ˆ˜' ,COUNT(*)
 FROM insa
 WHERE MOD( SUBSTR( ssn, -7, 1) , 2) = 0;


   
   -- DECODE ์‚ฌ์šฉ : MOD(), SUBSTR(), DECODE(), COUNT()               ์•”๊ธฐ
 

SELECT COUNT(*) ์ด์‚ฌ์›์ˆ˜
      ,  COUNT( DECODE(MOD( SUBSTR( ssn, -7, 1) , 2), 1, 100) ) ๋‚จ์ž์‚ฌ์›์ˆ˜
      , COUNT( DECODE(MOD( SUBSTR( ssn, -7, 1) , 2), 0, 0) ) ์—ฌ์ž์‚ฌ์›์ˆ˜
FROM insa;


   -- 1) COUNT( )   NULL ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ***
   -- 2) DECODE(          A,                  B,  C )    null
        DECODE(MOD( SUBSTR( ssn, -7, 1) , 2), 1, 100)
        
    (๋ฌธ์ œ)   CASE ํ•จ์ˆ˜ ์‚ฌ์šฉํ•ด์„œ ์ฝ”๋”ฉ.

    SELECT
        COUNT(*) ์ด์‚ฌ์›์ˆ˜
       ,COUNT(
         CASE  MOD( SUBSTR( ssn, -7, 1) , 2)
          WHEN  1 THEN  '๋‚จ์ž'
          -- ELSE NULL
         END
       ) ๋‚จ์ž์‚ฌ์›์ˆ˜
       ,COUNT(
         CASE  MOD( SUBSTR( ssn, -7, 1) , 2)
          WHEN  0 THEN  '์—ฌ์ž'
          -- ELSE NULL
         END
         ) ์—ฌ์ž์‚ฌ์›์ˆ˜
    FROM insa;


    
    (๋ฌธ์ œ) emp ํ…Œ์ด๋ธ”์—์„œ ๊ฐ ๋ถ€์„œ๋ณ„ ์‚ฌ์›์ˆ˜ ์ถœ๋ ฅ(์กฐํšŒ)  - DECODE ํ•จ์ˆ˜ ์‚ฌ์šฉ

SELECT COUNT(*)
FROM emp
UNION ALL
SELECT COUNT(*)
FROM emp
WHERE deptno =10
UNION ALL
SELECT COUNT(*)
FROM emp
WHERE deptno = 20
UNION ALL
SELECT COUNT(*)
FROM emp
WHERE deptno =30
UNION ALL
SELECT COUNT(*)
FROM emp
WHERE deptno =40;


    
    -- DECODE

SELECT
        COUNT(*)  ์ด์‚ฌ์›์ˆ˜
       ,  COUNT(DECODE( deptno, 10, 'O' )) "10์‚ฌ์›์ˆ˜" 
       ,  COUNT(DECODE( deptno, 20, 'O' )) "20์‚ฌ์›์ˆ˜"
       ,  COUNT(DECODE( deptno, 30, 'O' )) "30์‚ฌ์›์ˆ˜" 
       ,  COUNT(DECODE( deptno, 40, 'O' )) "40์‚ฌ์›์ˆ˜"
       -- ๊ฐ ๋ถ€์„œ๋ณ„ ์ด ๊ธ‰์—ฌํ•ฉ ์ถœ๋ ฅ
       ,SUM(DECODE(  deptno, 10, sal + NVL(comm, 0) ))  ๊ธ‰์—ฌํ•ฉ_10
       ,SUM(DECODE(  deptno, 20, sal + NVL(comm, 0) ))  ๊ธ‰์—ฌํ•ฉ_20
       ,SUM(DECODE(  deptno, 30, sal + NVL(comm, 0) ))  ๊ธ‰์—ฌํ•ฉ_30
       ,NVL( SUM(DECODE(  deptno, 40, sal + NVL(comm, 0) )) , 0 )   ๊ธ‰์—ฌํ•ฉ_40
FROM emp;


      
]
    
   -- ๊ฐ ๋ถ€์„œ๋ณ„ ์ด ๊ธ‰์—ฌํ•ฉ ์ถœ๋ ฅ

SELECT
        SUM(DECODE(  deptno, 10, sal + NVL(comm, 0) ))  ๊ธ‰์—ฌํ•ฉ_10
       ,SUM(DECODE(  deptno, 20, sal + NVL(comm, 0) ))  ๊ธ‰์—ฌํ•ฉ_20
       ,SUM(DECODE(  deptno, 30, sal + NVL(comm, 0) ))  ๊ธ‰์—ฌํ•ฉ_30
       ,NVL( SUM(DECODE(  deptno, 40, sal + NVL(comm, 0) )) , 0 )   ๊ธ‰์—ฌํ•ฉ_40
FROM emp;


    

8. emp ์—์„œ  [์‚ฌ์›์ด ์กด์žฌํ•˜๋Š” ๋ถ€์„œ]์˜ ๋ถ€์„œ๋ฒˆํ˜ธ๋งŒ ์ถœ๋ ฅ

SELECT deptno
FROM dept;
--
SELECT DISTINCT deptno
FROM emp;



-- ๋น„์ „๊ณต์ž --

SELECT deptno
FROM (
    SELECT deptno  --, COUNT(*) a
    FROM emp
    GROUP BY deptno
)
WHERE a IS NOT NULL;
--
SELECT deptno      -- ์ด๋ ‡๊ฒŒ๋งŒํ•ด๋„ ๋‚˜์˜ด!
FROM emp
GROUP BY deptno




  (๋ฌธ์ œ) ์‚ฌ์›์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ถ€์„œ๋ฒˆํ˜ธ๋งŒ ์ถœ๋ ฅ.

SELECT deptno
FROM dept
MINUS
SELECT DISTINCT deptno
FROM emp;



  -- JOIN(์กฐ์ธ) --

9. ํ•จ์ˆ˜ ์„ค๋ช… (์ •๋ฆฌํ•ด๋‘๊ธฐ)

    9-1. NULLIF() ํ•จ์ˆ˜ ์„ค๋ช…
    9-2. COALESCE() ํ•จ์ˆ˜ ์„ค๋ช…
    9-3. DECODE() ํ•จ์ˆ˜ ์„ค๋ช…
    9-4. LAST_DAY() ํ•จ์ˆ˜ ์„ค๋ช…
    9-5. ADD_MONTHS() ํ•จ์ˆ˜ ์„ค๋ช…
    9-6. MONTHS_BETWEEN() ํ•จ์ˆ˜ ์„ค๋ช…
    9-7. NEXT_DAY() ํ•จ์ˆ˜ ์„ค๋ช…
    9-8. EXTRACT() ํ•จ์ˆ˜ ์„ค๋ช…
    
     ใ„ฑ. ASCII() ํ•จ์ˆ˜ ์„ค๋ช…
     ใ„ด. CHR() ํ•จ์ˆ˜ ์„ค๋ช…
     ใ„ท. GREATEST() ํ•จ์ˆ˜ ์„ค๋ช…
     ใ„น. LEAST() ํ•จ์ˆ˜ ์„ค๋ช…
     ใ…. UPPER() ํ•จ์ˆ˜ ์„ค๋ช…
     ใ…‚. LOWER() ํ•จ์ˆ˜ ์„ค๋ช…
     ใ……. LENGTH() ํ•จ์ˆ˜ ์„ค๋ช…
     ใ…‡. SUBSTR() ํ•จ์ˆ˜ ์„ค๋ช…
     ใ…ˆ. INSTR() ํ•จ์ˆ˜ ์„ค๋ช…  X
     
-- SQL + PL
    ๊ฐ ์‚ฌ์›์˜ pay๋ฅผ ๋ฐฑ๋ถ„๋ฅ ๋กœ ๊ณ„์‚ฐํ•ด์„œ 10% ๋‹น ๋ณ„ํ•˜๋‚˜(*)๋กœ ์ฒ˜๋ฆฌํ•ด์„œ ์ถœ๋ ฅ
   ( ์†Œ์ˆซ์  ์ฒซ ์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•ด์„œ ์ถœ๋ ฅ )

SELECT t.*   
       , t.pay / t.max_pay * 100 || '%' "percent"
       , ROUND( t.pay / t.max_pay * 100/10 )  ๋ณ„๊ฐฏ์ˆ˜
       , RPAD( ' ',  ROUND( t.pay / t.max_pay * 100 /10) + 1 , '*' )
FROM (    
        SELECT ename
           , sal + NVL( comm, 0 ) pay   
           , ( SELECT MAX( sal + NVL( comm, 0 ) )     FROM emp  ) max_pay
        FROM emp   
)  t;



11. ์•„๋ž˜ ์ฝ”๋”ฉ์„  DECODE()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ‘œํ˜„ํ•˜์„ธ์š”.
    ใ„ฑ. [์ž๋ฐ”]
        if( A == B ){
           return X;
        }
        
        DECODE( A, B, X )
    
    ใ„ด. [์ž๋ฐ”]
        if( A==B){
           return S;
        }else if( A == C){
           return T;
        }else{
           return U;
        }
    
    DECODE( A, B, S, C, T, U )
    
    ใ„ท.  [์ž๋ฐ”]
        if( A==B){
           return XXX;
        }else{
           return YYY;
        }
        
         DECODE( A, B, XXX, YYY )
         
12. insaํ…Œ์ด๋ธ”์—์„œ 1001, 1002 ์‚ฌ์›์˜ ์ฃผ๋ฏผ๋ฒˆํ˜ธ์˜ ์›”/์ผ ๋งŒ 10์›”05์ผ๋กœ ์ˆ˜์ •ํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑ 
SELECT *
FROM insa;

77[1212]-1022432
80[1004]-1544236

UPDATE insa
SET ssn = SUBSTR( ssn, 0, 2)  ||     '1005'  ||   SUBSTR( ssn, -8)
WHERE num IN ( 1001, 1002);

COMMIT;



12-2. insaํ…Œ์ด๋ธ”์—์„œ '2022.10.05'์„ ๊ธฐ์ค€์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ.  

๊ฒฐ๊ณผ)
์žฅ์ธ์ฒ    780506-1625148   ์ƒ์ผ ํ›„
๊น€์˜๋…„   821011-2362514   ์ƒ์ผ ์ „
๋‚˜์œค๊ท    810810-1552147   ์ƒ์ผ ํ›„
๊น€์ข…์„œ   751010-1122233   ์˜ค๋Š˜ ์ƒ์ผ
์œ ๊ด€์ˆœ   801010-2987897   ์˜ค๋Š˜ ์ƒ์ผ
์ •ํ•œ๊ตญ   760909-1333333   ์ƒ์ผ ํ›„

 -- ORA-01861: literal does not match format string
 

SELECT name, ssn
  , TO_DATE(  SUBSTR( ssn, 3, 4) , 'MMDD' ) 
  , TRUNC(  SYSDATE )
  , DECODE(  SIGN( TO_DATE(  SUBSTR( ssn, 3, 4) , 'MMDD' )  -  TRUNC(  SYSDATE ) )
               , 0 , '์˜ค๋Š˜ ์ƒ์ผ'
               , 1 , '์ƒ์ผ ์ „'
               , -1, '์ƒ์ผ ํ›„' 
               ) a
  , CASE  SIGN( TO_DATE(  SUBSTR( ssn, 3, 4) , 'MMDD' )  -  TRUNC(  SYSDATE ) )
      WHEN 1 THEN '์ƒ์ผ ์ „'
      WHEN -1 THEN '์ƒ์ผ ํ›„' 
      ELSE  '์˜ค๋Š˜ ์ƒ์ผ'
    END b
FROM insa;



12-3. insaํ…Œ์ด๋ธ”์—์„œ '2022.10.05'๊ธฐ์ค€์œผ๋กœ ์ด ๋‚ ์ด ์ƒ์ผ์ธ ์‚ฌ์›์ˆ˜,์ง€๋‚œ ์‚ฌ์›์ˆ˜, ์•ˆ ์ง€๋‚œ ์‚ฌ์›์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ. 
-- 1)

SELECT 
            COUNT(  DECODE( s , 1 , 1 ) ) "์ƒ์ผ ์ „ ์‚ฌ์›์ˆ˜"
          , COUNT(  DECODE( s , -1, 1 ) ) "์ƒ์ผ ํ›„ ์‚ฌ์›์ˆ˜"
          , COUNT(  DECODE( s , 0, 1 ) ) "์˜ค๋Š˜ ์ƒ์ผ ์‚ฌ์›์ˆ˜"
FROM (
            SELECT name, ssn
                , SIGN( TO_DATE(  SUBSTR( ssn, 3, 4) , 'MMDD' )  -  TRUNC(  SYSDATE ) ) s
            FROM insa
) t;



--2)

SELECT s
       , CASE   s 
              WHEN 1 THEN  '์ƒ์ผ ์ „ ์‚ฌ์›์ˆ˜'
              WHEN 0 THEN  '์˜ค๋Š˜ ์ƒ์ผ ์‚ฌ์›์ˆ˜'
              WHEN -1 THEN '์ƒ์ผ ํ›„ ์‚ฌ์›์ˆ˜'
           END "์‚ฌ์›์ˆ˜"
       , COUNT(*) 
FROM (
            SELECT name, ssn
                , SIGN( TO_DATE(  SUBSTR( ssn, 3, 4) , 'MMDD' )  -  TRUNC(  SYSDATE ) ) s
            FROM insa
) t
GROUP BY  s ; -- 1 , 0 , -1



13.  emp ํ…Œ์ด๋ธ”์—์„œ 

                 10๋ฒˆ ๋ถ€์„œ์›๋“ค์€  ๊ธ‰์—ฌ 15% ์ธ์ƒ              1.15
                20๋ฒˆ ๋ถ€์„œ์›๋“ค์€ ๊ธ‰์—ฌ 10% ์ธ์ƒ
                30๋ฒˆ ๋ถ€์„œ์›๋“ค์€ ๊ธ‰์—ฌ 5% ์ธ์ƒ
                40๋ฒˆ ๋ถ€์„œ์›๋“ค์€ ๊ธ‰์—ฌ 20% ์ธ์ƒ
  ํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ.     
  

  -- ORA-00972: identifier is too long
  SELECT deptno, ename, sal + NVL(comm, 0) pay
         ,   DECODE( deptno, 10 , 15 , 20, 10, 30, 5, 40, 20)  || '%'  "์ธ์ƒ๋ฅ "
         ,   (sal + NVL(comm, 0)) * DECODE( deptno, 10 , 15 , 20, 10, 30, 5, 40, 20) /100  "์ธ์ƒ์•ก"
 --        ,   (sal + NVL(comm, 0)) + (sal + NVL(comm, 0)) * DECODE( deptno, 10 , 15 , 20, 10, 30, 5, 40, 20) /100  "์ธ์ƒ๋œ Pay"
 --        ,   (sal + NVL(comm, 0))*(1 +  DECODE(deptno, 10, 15, 20, 10, 30, 5, 40, 20) / 100)  "์ธ์ƒ๋œ Pay"         
         ,   (sal + NVL(comm, 0)) *DECODE( deptno, 10 , 1.15 , 20, 1.10, 30, 1.05, 40, 1.20)  "์ธ์ƒ๋œ Pay"   
  FROM emp
  ORDER BY deptno ASC;


          
14. emp ํ…Œ์ด๋ธ”์—์„œ ๊ฐ ๋ถ€์„œ์˜ ์‚ฌ์›์ˆ˜๋ฅผ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ

SELECT COUNT(*)
   , COUNT(  DECODE(deptno, 10, 1) )  ๋ถ€์„œ10
   , COUNT(  DECODE(deptno, 20, 1) )  ๋ถ€์„œ20
   , COUNT(  DECODE(deptno, 30, 1) )  ๋ถ€์„œ30
   , COUNT(  DECODE(deptno, 40, 1) )  ๋ถ€์„œ40
FROM emp;


--
-- 1) ์ด์‚ฌ์›์ˆ˜ , 2) ์‚ฌ์›์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋ถ€์„œ ์ •๋ณด X ( 40 0 ) ์ถœ๋ ฅ. -  ์ „๊ณต์ž๋“ค..

-- ORA-00904: "DEPTNO": invalid identifier
--       ์ปฌ๋Ÿผ๊ฐฏ์ˆ˜, ์ž๋ฃŒํ˜• ๋™์ผ( ์ฃผ์˜์‚ฌํ•ญ )
--       ์ฒซ ๋ฒˆ์งธ SELECT 0 deptno ์ปฌ๋Ÿผ๋ช… ๋˜๋Š” ๋ณ„์นญ,  COUNT(*)

SELECT 0 deptno,  COUNT(*)
FROM emp
UNION ALL
SELECT deptno, COUNT(*) 
FROM emp
GROUP BY deptno
ORDER BY deptno ASC;



-- ๋‹ต : OUTER JOIN ( ๋ฉด์ ‘ ์‹ค๊ธฐ )
--  TOP N ๋ฐฉ์‹ == ์ƒ์œ„ N 

 

SELECT 0  deptno,  COUNT(*)
FROM emp
UNION ALL
SELECT  deptno, COUNT(*) 
FROM emp  
GROUP BY deptno
ORDER BY deptno ASC;


-- 40  0 X
--INNER JOIN , OUTER JOIN
--  ORA-00918: column ambiguously defined
--             ์ปฌ๋Ÿผ์ด    ์• ๋งค๋ชจํ˜ธํ•˜๊ฒŒ ์„ ์–ธ๋˜์—ˆ๋‹ค.
-- ( ์กฐ์ธ ํ•  ๋•Œ ๊ณตํ†ต์ ์ธ ์ปฌ๋Ÿผ๋ช…์€   ๋ณ„์นญ.์ปฌ๋Ÿผ๋ช… )
-- ( ์•”๊ธฐ )

SELECT  d.deptno, COUNT(   e.deptno    ) 
FROM emp e ,   dept  d  -- RIGHT OUTER JOIN
-- FROM [ dept d ], emp e  LEFT [OUTER] JOIN
WHERE  e.deptno(+) = d.deptno -- ์กฐ์ธ์กฐ๊ฑด
GROUP BY d.deptno 
ORDER BY d.deptno ASC;

 

-- [ JOIN ] ์‹œํ—˜
-- ๋ถ€์„œ๋ช…,์‚ฌ์›๋ช…, ์žก, ์ž…์‚ฌ์ผ์ž ์กฐํšŒ(์ถœ๋ ฅ)
--  dept : dname
--  emp  : ename, job, hiredate
-- RDBMS    dept    ์†Œ์†๊ด€๊ณ„          emp
--         PK:deptno      ์ฐธ์กฐ        FK:deptno
--  ๋ถ€๋ชจ์˜ PK  ์™€ ์ž์‹์˜ FK  ์กฐ์ธ์กฐ๊ฑด์ด๋œ๋‹ค. 

SELECT deptno, ename, job,hiredate
FROM emp;
 +
SELECT deptno, dname, loc
FROM dept;



--
--SELECT d.dname, e.ename, e.job, e.hiredate
-- ์กฐ์ธ ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

SELECT dname, ename, job, hiredate
FROM emp  e , dept  d
WHERE  d.deptno   = e.deptno;  -- ์กฐ์ธ ์กฐ๊ฑด


-- ์กฐ์ธ ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ• (  JOIN  ~  ON  ๊ตฌ๋ฌธ )

SELECT dname, ename, job, hiredate
FROM emp  e JOIN dept  d    ON  d.deptno   = e.deptno;



15. emp, salgrade ๋‘ ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•ด์„œ ์•„๋ž˜ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ์ฟผ๋ฆฌ ์ž‘์„ฑ.
1   700   1200
2   1201   1400
3   1401   2000
4   2001   3000
5   3001   9999
-- (๋‚ด์ผ ์‹œํ—˜)

SELECT ename, sal
    , CASE 
          WHEN sal >= 700 AND sal <= 1200 THEN 1
          WHEN sal BETWEEN 1201 AND 1400 THEN 2
          WHEN sal BETWEEN 1401 AND 2000 THEN 3
          WHEN sal BETWEEN 2001 AND 3000 THEN 4
          WHEN sal BETWEEN 3001 AND 9999 THEN 5
      END GRADE
FROM emp;


-- (๋‚ด์ผ ์‹œํ—˜) JOIN

-- ์กฐ์ธ ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

SELECT ename, sal, losal || ' ~ ' || hisal, grade
FROM emp , salgrade
WHERE  sal BETWEEN losal AND hisal;  -- ์กฐ์ธ ์กฐ๊ฑด


-- ์กฐ์ธ ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

SELECT ename, sal, losal || ' ~ ' || hisal, grade
FROM emp JOIN salgrade ON sal BETWEEN losal AND hisal;  -- ์กฐ์ธ ์กฐ๊ฑด



ENAME   SAL     GRADE
----- ----- ---------
SMITH   800       1
ALLEN   1900   3
WARD   1750   3
JONES   2975   4
MARTIN   2650   4
BLAKE   2850   4
CLARK   2450   4
KING   5000   5
TURNER   1500   3
JAMES   950       1
FORD   3000   4
MILLER   1300   2



SELECT *
FROM salgrade; -- ๊ธ‰์—ฌ ๋“ฑ๊ธ‰ ํ…Œ์ด๋ธ”


[์กฐ์ธ๋ฌธ์ œ] dname, ename, sal, grade  ์กฐํšŒ
dept     : dname
emp     : ename, sal
salgrade : grade
-- ์กฐ์ธ ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

SELECT dname, ename, sal, grade
FROM dept d, emp e, salgrade s
WHERE  d.deptno = e.deptno  AND   e.sal BETWEEN s.losal AND s.hisal; -- ์กฐ์ธ ์กฐ๊ฑด



-- ์กฐ์ธ ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

SELECT dname, ename, sal, grade
FROM dept d  JOIN emp e      ON  d.deptno = e.deptno  
             JOIN salgrade s ON e.sal BETWEEN s.losal AND s.hisal;



16. emp ํ…Œ์ด๋ธ”์—์„œ ๊ธ‰์—ฌ๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ›๋Š” ์‚ฌ์›์˜ empno, ename, pay ๋ฅผ ์ถœ๋ ฅ.

SELECT empno, ename, sal
FROM emp
WHERE sal >= ALL ( SELECT sal  FROM emp );
WHERE sal = ( SELECT MAX(sal)  FROM emp );



16-2. emp ํ…Œ์ด๋ธ”์—์„œ ๊ฐ ๋ถ€์„œ๋ณ„ ๊ธ‰์—ฌ๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ›๋Š” ์‚ฌ์›์˜ pay๋ฅผ ์ถœ๋ ฅ
 1) UNION ALL
 2) GROUP BY

SELECT  deptno, MAX(sal + NVL(comm, 0 )) max_pay
FROM emp
GROUP BY deptno
ORDER BY deptno ASC;


  3) ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ
  -- 596ํ–‰, 3์—ด์—์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ   ORA-00907: missing right parenthesis

SELECT deptno, ename, sal + NVL( comm,0 )
FROM emp  a
WHERE sal + NVL( comm,0 ) = ( SELECT MAX( sal + NVL( comm,0 ) )   FROM emp b  WHERE b.deptno = a.deptno    ) ;

 



  [๋ฌธ์ œ] emp ํ…Œ์ด๋ธ”์—์„œ pay๋ฅผ ๋งŽ์ด ๋ฐ›๋Š” 3๋ช… ์ •๋ณด ์กฐํšŒ

 

( TOP - N ๋ฐฉ์‹ )  

• top-N ๋ถ„์„์€ ์ตœ๋Œ€๊ฐ’์ด๋‚˜ ์ตœ์†Œ๊ฐ’์„ ๊ฐ€์ง„ ์ปฌ๋Ÿผ์„ ์งˆ์˜ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” ๋ถ„์„๋ฐฉ๋ฒ•์ด๋‹ค.
• inline view์—์„œ ORDER BY ์ ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์›ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ •๋ ฌ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
• ROWNUM ์ปฌ๋Ÿผ์€ subquery์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ ํ–‰์— ์ˆœ์ฐจ์ ์ธ ๋ฒˆํ˜ธ๋ฅผ ๋ถ€์—ฌํ•˜๋Š” pseudo ์ปฌ๋Ÿผ์ด๋‹ค.
• n๊ฐ’์€ < ๋˜๋Š” >=๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜ํ•˜๋ฉฐ, ๋ฐ˜ํ™˜๋  ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•œ๋‹ค.


ใ€ํ˜•์‹ใ€‘

   SELECT ์ปฌ๋Ÿผ๋ช…,..., ROWNUM
   FROM (SELECT ์ปฌ๋Ÿผ๋ช…,... from ํ…Œ์ด๋ธ”๋ช…
         ORDER BY top_n_์ปฌ๋Ÿผ๋ช…)
        WHERE ROWNUM <= n;
   SELECT  ROWNUM  ,     t.*
   FROM (
            SELECT deptno, ename, job, sal + NVL(comm, 0) pay
             FROM emp  
             ORDER BY pay DESC
         )    t 
   --WHERE ROWNUM <= 1;         
   WHERE ROWNUM <= 3;

 


   WHERE ROWNUM BETWEEN 3 AND 5 ; X  ์ค‘๊ฐ„์˜ ์ˆœ๋ฒˆ ํ–‰์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋Š” ์—†๋‹ค. ( ์ฃผ์˜ )



 [๋ฌธ์ œ] emp ํ…Œ์ด๋ธ”์—์„œ pay๋ฅผ ๋งŽ์ด ๋ฐ›๋Š” 3๋ช… ์ •๋ณด ์กฐํšŒ ( RANK ๊ด€๋ จ ํ•จ์ˆ˜ )  

 

ROW_NUMBER ํ•จ์ˆ˜
- ์ด ํ•จ์ˆ˜๋Š” ๋ถ„์„(analytic) ํ•จ์ˆ˜๋กœ์„œ, ๋ถ„ํ• ๋ณ„๋กœ ์ •๋ ฌ๋œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ์ˆœ์œ„๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.
- ๋ถ„ํ• ์€ ์ „์ฒด ํ–‰์„ ํŠน์ • ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ GROUP BY ์ ˆ์—์„œ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ฐ™์€ ๊ฐœ๋…์ด๋‹ค.


ใ€ํ˜•์‹ใ€‘

ROW_NUMBER () 
OVER ([query_partition_clause] order_by_clause )


 ๋ถ€์„œ๋ณ„๋กœ ํŒŒํ‹ฐ์…˜ ๊ตฌ๋ถ„ - pay ์ •๋ ฌ + ์ˆœ์œ„

t.seq = 1๋กœ ๋‘๋ฉด ๊ฐ ๋ถ€์„œ๋ณ„ 1๋“ฑ ํŽธ๋ฆฌํ•˜๊ฒŒ ๊ตฌํ•ด์˜ฌ์ˆ˜์žˆ์Œ!!

 SELECT t.*
 FROM (
      SELECT deptno, ename, sal + NVL(comm,0) pay
          , ROW_NUMBER() OVER( PARTITION BY deptno ORDER BY  sal + NVL(comm,0) DESC)  seq  -- pay ์ˆœ์œ„ ๋งค๊ธฐ๊ธฐ
      FROM emp
  ) t 
 WHERE t.seq = 1 ;


๋ถ€์„œ๋ณ„ ์ˆœ์œ„ ์ถœ๋ ฅ

SELECT t.*
 FROM (
      SELECT deptno, ename, sal + NVL(comm,0) pay
          , ROW_NUMBER() OVER( ORDER BY  sal + NVL(comm,0) DESC)  seq  -- pay ์ˆœ์œ„ ๋งค๊ธฐ๊ธฐ
      FROM emp
  ) t
 WHERE t.seq BETWEEN 3 AND 5; 
 WHERE t.seq <= 3 ; 
 WHERE t.seq = 1 ;

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ