[Day8] Oracle 8 [10/6]

 

--1. emp , salgrade ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ. [JOIN] ์‚ฌ์šฉ
--
--    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

ใ„ฑ. ์ •์˜ : ๊ฐ™๊ฑฐ๋‚˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ”์—์„œ ์ปฌ๋Ÿผ์„ ๊ฒ€์ƒ‰ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค
    emp      : ename   sal
    salgrade : grade
ใ„ด. WHERE ์กฐ์ธ ์กฐ๊ฑด
ใ„ท. 8 ๊ฐ€์ง€ ์ข…๋ฅ˜
ใ„น. ํ˜•์‹
  1)

  SELECT ename, sal, grade
  FROM emp, salgrade
  WHERE sal BETWEEN losal AND hisal;


  2) JOIN ~ ON ๊ตฌ๋ฌธ

  SELECT ename, sal, grade
  FROM emp JOIN salgrade  ON sal BETWEEN losal AND hisal;


  3) JOIN X   -  CASE ํ•จ์ˆ˜ ์‚ฌ์šฉ.

  SELECT ename, sal
    , CASE 
             WHEN sal BETWEEN 700 AND 1200  THEN 1
             , WHEN sal BETWEEN 700 AND 1200  THEN 1
             , WHEN sal BETWEEN 700 AND 1200  THEN 1
             , WHEN sal BETWEEN 700 AND 1200  THEN 1
             , WHEN sal BETWEEN 700 AND 1200  THEN 1
      END grade
  FROM emp;


  
  [๋ฌธ์ œ] deptno, dname, ename, hiredate, sal , grade ์ปฌ๋Ÿผ ์กฐํšŒ
  
  emp      : [deptno] , ename, hiredate, sal
  dept     : [deptno], dname
  salgrade : grade
  
  1)

  SELECT dept.deptno, dname, ename, hiredate, sal , grade
  FROM emp, dept, salgrade
  WHERE  emp.deptno = dept.deptno AND sal BETWEEN losal AND hisal;


  2)

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


  3)  ํ…Œ์ด๋ธ”์˜ ๋ณ„์นญ์„ ์‚ฌ์šฉํ•ด์„œ ์ž‘์„ฑ

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


  
--1-2. ์œ„์˜ ๊ฒฐ๊ณผ์—์„œ ๋“ฑ๊ธ‰(grade)๊ฐ€ 1๋“ฑ๊ธ‰์ธ ์‚ฌ์›๋งŒ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ  
--  ( ์กฐ๊ฑด :  TOP-N ๋ฐฉ์‹ ์‚ฌ์šฉ )
--
--๊ฒฐ๊ณผ)
--     EMPNO ENAME             SAL      GRADE
------------ ---------- ---------- ----------
--      7369 SMITH             800          1
--      7900 JAMES             950          1      

 

 SELECT t.empno, t.ename, t.sal, t.grade
 FROM (
      SELECT empno, ename, sal, grade
          , RANK() OVER( ORDER BY  grade)  seq
      FROM emp , salgrade
      WHERE  sal BETWEEN losal AND hisal
  ) t 
 WHERE t.seq = 1 ;
  SELECT t.*
  FROM ( 
      SELECT empno, ename, sal, grade
      FROM emp, salgrade
      WHERE sal BETWEEN losal AND hisal
  ) t
  WHERE grade = 1;



  
--      
--2. emp ์—์„œ ์ตœ๊ณ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์›์˜ ์ •๋ณด ์ถœ๋ ฅ ( JOIN ~ ON ๊ตฌ๋ฌธ )
--  ( ์กฐ๊ฑด : ์•„๋ž˜ ์ปฌ๋Ÿผ ์ถœ๋ ฅ )
--DNAME          ENAME             PAY
---------------- ---------- ----------
--ACCOUNTING     KING             5000

1)
WITH 
   temp AS (
          SELECT  ename, deptno, sal+NVL(comm,0) pay
          FROM emp
          WHERE sal = ( SELECT MAX(sal)  FROM emp )
  )
SELECT dname, ename, pay
FROM temp t JOIN dept d ON t.deptno = d.deptno;

2)
SELECT t.*
FROM(
    SELECT dname, ename, sal+NVL(comm,0) pay
    FROM emp e JOIN dept d ON e.deptno = d.deptno
) t
WHERE t.pay = (SELECT MAX(sal+NVL(comm,0)) FROM emp);


-- 1) 

SELECT dname, ename , sal + NVL(comm, 0) pay
FROM dept, emp
WHERE dept.deptno = emp.deptno  AND sal + NVL(comm, 0) = ( SELECT MAX( sal + NVL(comm, 0) ) max_pay
FROM emp );


-- 2)

SELECT dname, ename , sal + NVL(comm, 0) pay
FROM dept d  JOIN  emp e  ON d.deptno = e.deptno
WHERE sal + NVL(comm, 0) = ( SELECT MAX( sal + NVL(comm, 0) ) max_pay FROM emp );


--  ์ˆœ์œ„ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ• : RANK(), ROW_NUMBER(), TOP-N ๋ฐฉ์‹

--2-2. emp ์—์„œ ๊ฐ ๋ถ€์„œ๋ณ„ ์ตœ๊ณ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์›์˜ ์ •๋ณด ์ถœ๋ ฅ ( JOIN )
--
--    DEPTNO DNAME          ENAME             PAY
------------ -------------- ---------- ----------
--        10 ACCOUNTING     KING             5000
--        20 RESEARCH       FORD             3000
--        30 SALES          BLAKE            2850

 


   1) ์ƒ๊ด€ ์„œ๋ธŒ์ฟผ๋ฆฌ  + JOIN

   SELECT d.deptno, dname, ename , sal + NVL(comm, 0) pay
   FROM dept d  JOIN  emp e  ON d.deptno = e.deptno
   WHERE sal + NVL(comm, 0) = (  
                                   SELECT MAX( sal + NVL( comm, 0) ) max_pay
                                   FROM emp 
                                   WHERE deptno = e.deptno
                              );

                     
   2) RANK ์ˆœ์œ„ ๊ด€๋ จ ํ•จ์ˆ˜ - ROW_NUMBER()
   -- ๊ฐ ๋ถ€์„œ๋ณ„ ์ตœ๊ธ‰ pay ๋ฐ›๋Š” ์‚ฌ์›์˜ ์ •๋ณด ์กฐํšŒ
   -- ใ„ฑ. [๊ฐ ๋ถ€์„œ๋ณ„ pay ๋ณ„ ์ˆœ์œ„] ๋งค๊ฒจ์„œ
   -- ใ„ด. ์กฐ๊ฑด 1๋“ฑ ๋งŒ ์กฐํšŒ
   
   ใ€ํ˜•์‹ใ€‘
      ROW_NUMBER () 
                   OVER ([query_partition_clause] order_by_clause )

SELECT e.deptno, d.dname , ename, pay, seq
   FROM (
           SELECT deptno, ename, sal + NVL(comm, 0) pay
               , ROW_NUMBER() OVER( PARTITION BY deptno  ORDER BY sal + NVL(comm, 0) DESC )  seq
           FROM emp
        ) e  JOIN dept d ON d.deptno = e.deptno
   WHERE seq = 1;
SELECT deptno, dname, ename, pay
FROM(
    SELECT d.deptno, dname, ename, sal+NVL(comm,0) pay
     ,ROW_NUMBER() OVER( PARTITION BY d.deptno ORDER BY sal+NVL(comm,0) DESC)  seq
    FROM emp e JOIN dept d ON e.deptno = d.deptno
) t
 WHERE t.seq = 1 ;

  
   3) TOP-N ๋ฐฉ์‹

   SELECT t.*,  ROWNUM seq
   FROM (
           SELECT e.deptno, dname, ename, sal + NVL( comm, 0) pay
           FROM emp e JOIN dept d ON e.deptno = d.deptno
           ORDER BY  pay DESC
   ) t

 


   
   -- RANK ํ•จ์ˆ˜  -- : ์ •๋ฆฌ : https://crosstheline.tistory.com/104
   1) DENSE_RANK ํ•จ์ˆ˜
    ๊ทธ๋ฃน ๋‚ด์—์„œ ์ฐจ๋ก€๋กœ ๋œ ํ–‰์˜ rank๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ NUMBER ๋ฐ์ดํ„ฐํƒ€์ž…์œผ๋กœ ์ˆœ์œ„๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
     ํ•ด๋‹น ๊ฐ’์— ๋Œ€ํ•œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •(์ค‘๋ณต ์ˆœ์œ„ ๊ณ„์‚ฐ ์•ˆํ•จ) 
    

    ใ€Aggregate ํ˜•์‹ใ€‘
          DENSE_RANK ( expr[,expr,...] ) WITHIN GROUP
            (ORDER BY expr [[DESC ¦ ASC] [NULLS {FIRST ¦ LAST} , expr,...] )
       
    ใ€Analytic ํ˜•์‹ใ€‘
          DENSE_RANK ( ) OVER ([query_partion_clause] order_by_clause )


      
   2)   PERCENT_RANK ํ•จ์ˆ˜
        ์ด ํ•จ์ˆ˜๋Š” CUME_DIST ํ•จ์ˆ˜์™€ ์œ ์‚ฌํ•˜๊ฒŒ percent_rank ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

        ใ€aggregate ํ˜•์‹ใ€‘
              PERCENT_RANK(expr,...) WITHIN GROUP 
                (ORDER BY expr { [DESC ¦ ASC]
                                 [NULLS {FIRST ¦ LAST}]
                               },...)
        ๋˜๋Š” 
        ใ€Analytic ํ˜•์‹ใ€‘
               PERCENT_RANK() OVER ( 
                                     [query_partition_clause]
                                      order_by_clause


   3)RANK ํ•จ์ˆ˜ 
        ์ด ํ•จ์ˆ˜๋Š” ๊ทธ๋ฃน ๋‚ด์—์„œ ์œ„์น˜๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
        ํ•ด๋‹น ๊ฐ’์— ๋Œ€ํ•œ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฒฐ์ •(์ค‘๋ณต ์ˆœ์œ„ ๊ณ„์‚ฐํ•จ)
        
        ๋ฐ˜ํ™˜๋˜๋Š” ๋ฐ์ดํ„ฐํƒ€์ž…์€ NUMBER์ด๋‹ค.
        
   

   ใ€Aggregate ํ˜•์‹ใ€‘
                RANK(expr[,...]) WITHIN GROUP
                    (ORDER BY {expr [DESC ¦ ASC] [NULLS {FIRST ¦ LAST}]
                              } )
        
        ใ€Analytic ํ˜•์‹ใ€‘
            RANK() OVER( [query_partition_clause] order_by_clause
                           )



   4) FIRST, LAST
   5) ROW_NUMBER
                    rn  dr  r
   MARTIN   1250   9   9   9
   WARD   1250       10   9   9
   JAMES   950       11   10   11

   SELECT ename, sal
     , ROW_NUMBER() OVER( ORDER BY  sal DESC)  rn_seq
     , DENSE_RANK() OVER( ORDER BY  sal DESC)  dr_seq
     , RANK() OVER( ORDER BY  sal DESC)         r_seq
   FROM emp;


   

  [๋ฌธ์ œ]  emp ํ…Œ์ด๋ธ”์—์„œ   sal ๊ฐ€ ์ƒ์œ„ 20% ์‚ฌ์›์˜ ์ •๋ณด๋ฅผ ์กฐํšŒ.
  
  -- ์ด์‚ฌ์›์ˆ˜ 12๋ช…

 SELECT t.*
 FROM ( 
  SELECT deptno, ename, sal
     , RANK() OVER( ORDER BY sal DESC ) seq
  FROM emp
  ) t
  WHERE t.seq <=  ( 12*0.2 );


--  PERCENT_RANK() ๋ฐฑ๋ถ„๋ฅ  ํ•จ์ˆ˜

SELECT t.*
FROM (
  SELECT deptno, ename, sal
     , PERCENT_RANK() OVER( ORDER BY sal DESC ) seq
  FROM emp
) t
WHERe t.seq <= 0.2;


    
--3. emp ์—์„œ ๊ฐ ์‚ฌ์›์˜ ๊ธ‰์—ฌ๊ฐ€ ์ „์ฒด๊ธ‰์—ฌ์˜ ๋ช‡ %๊ฐ€ ๋˜๋Š” ์ง€ ์กฐํšŒ.
--       ( %   ์†Œ์ˆ˜์  3์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•˜์„ธ์š” )         ROUND( a[, b])
--            ๋ฌด์กฐ๊ฑด ์†Œ์ˆ˜์  2์ž๋ฆฌ๊นŒ์ง€๋Š” ์ถœ๋ ฅ.. 7.00%,  3.50%     
--           TO_CHAR( [NUMBER], DATE , VARCHAR2 )
--           \12,345
--ENAME             PAY   TOTALPAY ๋น„์œจ     
------------ ---------- ---------- -------
--SMITH             800      27125   2.95%
--ALLEN            1900      27125   7.00%
--WARD             1750      27125   6.45%
--JONES            2975      27125  10.97%
--MARTIN           2650      27125   9.77%
--BLAKE            2850      27125  10.51%
--CLARK            2450      27125   9.03%
--KING             5000      27125  18.43%
--TURNER           1500      27125   5.53%
--JAMES             950      27125   3.50%
--FORD             3000      27125  11.06%
--MILLER           1300      27125   4.79%

SELECT t.*
       , TO_CHAR (   ROUND(  t.pay / t.totalpay * 100 ,  2 ) ,  '999.00' )  || '%'  ๋น„์œจ
FROM ( 
        SELECT ename, sal + NVL( comm, 0) pay
             , ( SELECT SUM( sal + NVL( comm, 0)) FROM emp ) totalpay
        FROM emp
     ) t;


--4. emp ์—์„œ ๊ฐ€์žฅ ๋นจ๋ฆฌ ์ž…์‚ฌํ•œ ์‚ฌ์› ๊ณผ ๊ฐ€์žฅ ๋Šฆ๊ฒŒ(์ตœ๊ทผ) ์ž…์‚ฌํ•œ ์‚ฌ์›์˜ ์ฐจ์ด ์ผ์ˆ˜ ?         
--        

SELECT  MIN( hiredate )  
        , MAX( hiredate ) -- ์ตœ๊ทผ ์ž…์‚ฌ
        , ABS(  MIN( hiredate ) - MAX( hiredate )  )
FROM emp;


  --  FIRST_VALUE ํ•จ์ˆ˜  --  ํ˜„์žฌ ํ–‰๊นŒ์ง€์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’
FIRST_VALUE๋Š” ๋ถ„์„ ํ•จ์ˆ˜๋กœ ์ •๋ ฌ๋œ ๊ฐ’์ค‘์—์„œ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๋งŒ์•ฝ ์ฒซ๋ฒˆ์งธ๊ฐ€ NULL์ด๋ผ๋ฉด, IGNORE NULLS๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด NULL์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค.
์ฆ‰, ์œˆ๋„์šฐ์—์„œ ์ •๋ ฌ๋œ ๊ฐ’ ์ค‘์—์„œ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
LAST_VALUE ํ•จ์ˆ˜๋„ ๋ถ„์„ ํ•จ์ˆ˜๋กœ ์ •๋ ฌ๋œ ๊ฐ’์ค‘์—์„œ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.


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

   FIRST_VALUE ¦ LAST_VALUE (expr [IGNORE NULLS] )
    OVER (
      [PARTITION BY expr2] [,...]
      ORDER BY expr3 [collate_clause] [ASC ¦ DESC]
      [NULLS FIRST ¦ NULLS LAST])
   )


  --  LAST_VALUE ํ•จ์ˆ˜    -- ํ˜„์žฌ ํ–‰๊นŒ์ง€์˜ ๋งˆ์ง€๋ง‰ ๋ฒˆ์งธ ๊ฐ’
  

  SELECT
      --ename, hiredate
       FIRST_VALUE ( hiredate ) OVER( ORDER BY hiredate DESC  )  -- MAX(hiredate)
       , FIRST_VALUE ( hiredate ) OVER( ORDER BY hiredate ASC  )  -- MIN(hiredate)
      --, LAST_VALUE ( hiredate ) OVER( ORDER BY hiredate DESC  ) 
  FROM emp;



--5. insa ์—์„œ ์‚ฌ์›๋“ค์˜ ๋งŒ๋‚˜์ด ๊ณ„์‚ฐํ•ด์„œ ์ถœ๋ ฅ
--  ( ๋งŒ๋‚˜์ด = ์˜ฌํ•ด๋…„๋„ - ์ถœ์ƒ๋…„๋„          - 1( ์ƒ์ผ์ด์ง€๋‚˜์ง€ ์•Š์œผ๋ฉด) )

-- ๋งŒ๋‚˜์ด   = ์˜ฌํ•ด๋…„๋„ - ์ƒ์ผ๋…„๋„    (  -1  ์ƒ์ผ ์ง€๋‚˜์ง€ ์•Š์œผ๋ฉด  )   AmericanAge
--         = ์„ธ๋Š”๋‚˜์ด -1       [์ƒ์ผ์ง€๋‚จ์—ฌ๋ถ€ -1]
-- ์„ธ๋Š”๋‚˜์ด = ์˜ฌํ•ด๋…„๋„ - ์ƒ์ผ๋…„๋„ + 1            CountingAge
           -- ใ„ฑ 

SELECT t.name, t.ssn,  ใ„ฑ-ใ„ด+1           CountingAge
            , ใ„ฑ-ใ„ด + DECODE( isBCheck, -1, -1 , 0 )   AmericanAge
FROM (            
            SELECT name, ssn
               , TO_CHAR( SYSDATE, 'YYYY') ใ„ฑ
                -- ์„ฑ๋ณ„ 1,2,5,6  1900
                 --     3,4,7,8  2000
                 --      9,0      1800
               , SUBSTR( ssn, 0, 2 )  
               , CASE
                    WHEN SUBSTR( ssn, -7, 1)  IN ( 1,2,5,6 ) THEN 1900 + SUBSTR( ssn, 0, 2 )  
                    WHEN SUBSTR( ssn, -7, 1)  IN ( 3,4,7,8 ) THEN 2000 + SUBSTR( ssn, 0, 2 )  
                    WHEN SUBSTR( ssn, -7, 1)  IN ( 9,0 )     THEN 1800 + SUBSTR( ssn, 0, 2 )  
                 END  ใ„ด
               , SIGN(   TRUNC( SYSDATE) -  TO_DATE( SUBSTR( ssn, 3,4), 'MMDD') )  isBCheck  -- -1,0,1
            FROM insa
     ) t;



--6. insa ํ…Œ์ด๋ธ”์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ..
--     [์ด์‚ฌ์›์ˆ˜]      [๋‚จ์ž์‚ฌ์›์ˆ˜]      [์—ฌ์ž์‚ฌ์›์ˆ˜] [๋‚จ์‚ฌ์›๋“ค์˜ ์ด๊ธ‰์—ฌํ•ฉ]  [์—ฌ์‚ฌ์›๋“ค์˜ ์ด๊ธ‰์—ฌํ•ฉ] [๋‚จ์ž-max(๊ธ‰์—ฌ)] [์—ฌ์ž-max(๊ธ‰์—ฌ)]
------------ ---------- ---------- ---------- ---------- ---------- ----------
--        60                31              29           51,961,200                41,430,400                  2650000          2550000

SELECT  COUNT(*) ์ด์‚ฌ์›์ˆ˜
      , COUNT( DECODE(  MOD( SUBSTR( ssn, -7, 1), 2) , 1, 'O' )  )    ๋‚จ์ž์‚ฌ์›์ˆ˜
      , COUNT( DECODE(  MOD( SUBSTR( ssn, -7, 1), 2) , 0, 'X' )  )    ์—ฌ์ž์‚ฌ์›์ˆ˜
      , TO_CHAR( SUM( DECODE(  MOD( SUBSTR( ssn, -7, 1), 2) , 1, basicpay + sudang )  ) , 'L999,999,999' )  "๋‚จ์‚ฌ์›๋“ค์˜ ์ด๊ธ‰์—ฌํ•ฉ"
      , TO_CHAR(SUM( DECODE(  MOD( SUBSTR( ssn, -7, 1), 2) , 0, basicpay + sudang )  ) , 'L999,999,999' )  "์—ฌ์‚ฌ์›๋“ค์˜ ์ด๊ธ‰์—ฌํ•ฉ"
      , TO_CHAR(MAX( DECODE(  MOD( SUBSTR( ssn, -7, 1), 2) , 1, basicpay + sudang )  ) , 'L999,999,999' ) "๋‚จ์‚ฌ์›๋“ค์˜ MAX๊ธ‰์—ฌ"
      , TO_CHAR(MAX( DECODE(  MOD( SUBSTR( ssn, -7, 1), 2) , 0, basicpay + sudang )  ) , 'L999,999,999' )  "์—ฌ์‚ฌ์›๋“ค์˜ MAX๊ธ‰์—ฌ"
FROM insa;



--7. TOP-N ๋ฐฉ์‹์œผ๋กœ ํ’€๊ธฐ ( ROWNUM ์˜์‚ฌ ์ปฌ๋Ÿผ ์‚ฌ์šฉ )
--   emp ์—์„œ ์ตœ๊ณ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์›์˜ ์ •๋ณด ์ถœ๋ ฅ  
--  
--    DEPTNO ENAME             PAY   PAY_RANK
------------ ---------- ---------- ----------
--        10 KING             5000          1
--        
       

 SELECT  t.* , ROWNUM PAY_RANK
         FROM (
                         SELECT deptno, ename, sal + NVL(comm, 0) pay
                         FROM emp
                         ORDER BY pay DESC
              ) t
         WHERE ROWNUM = 1;                     
         WHERE ROWNUM <= 3;

   
         WHERE ROWNUM BETWEEN  3  AND 5;       -- ์ฃผ์˜
         WHERE ROWNUM > 3;                     -- ์ฃผ์˜ 
         -- WHERE PAY_RANK = 1;  -- ORA-00904: "PAY_RANK": invalid identifier


--8.์ˆœ์œ„(RANK) ํ•จ์ˆ˜ ์‚ฌ์šฉํ•ด์„œ ํ’€๊ธฐ 
--   emp ์—์„œ ๊ฐ ๋ถ€์„œ๋ณ„ ์ตœ๊ณ ๊ธ‰์—ฌ๋ฅผ ๋ฐ›๋Š” ์‚ฌ์›์˜ ์ •๋ณด ์ถœ๋ ฅ
--   
--    DEPTNO ENAME             PAY DEPTNO_RANK
------------ ---------- ---------- -----------
--        10 KING             5000           1
--        20 FORD             3000           1
--        30 BLAKE            2850           1
  ใ„ด.   + emp ์กฐ์ธ

SELECT t.deptno, e.ename, e.sal , e.hiredate , 1  AS DEPTNO_RANK
FROM (  
  SELECT deptno, MAX( sal ) maxpay
  FROM emp
  GROUP BY deptno
  ) t , emp e
WHERE t.deptno = e.deptno AND t.maxpay = e.sal;


        
  ใ„ฑ. ์ˆœ์œ„ ํ•จ์ˆ˜    

SELECT *
FROM (
        SELECT deptno, ename, sal + NVL(comm,0) pay
       , RANK() OVER( PARTITION BY deptno ORDER BY  sal + NVL(comm,0) DESC  ) deptno_rank
FROM emp
)
WHERE deptno_rank = 1;



--9. empํ…Œ์ด๋ธ”์—์„œ ๊ฐ ๋ถ€์„œ์˜ ์‚ฌ์›์ˆ˜, ๋ถ€์„œ์ด๊ธ‰์—ฌํ•ฉ, ๋ถ€์„œํ‰๊ท ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ.
--๊ฒฐ๊ณผ)
--    DEPTNO       ๋ถ€์„œ์›์ˆ˜       ์ด๊ธ‰์—ฌํ•ฉ            ํ‰๊ท 
------------ ----------       ----------    ----------
--        10          3          8750       2916.67
--        20          3          6775       2258.33
--        30          6         11600       1933.33      

SELECT 
     , COUNT( DECODE( 10 )
     , SUM( DECODE( 10 )
     , AVG( DECODE( 10 )
FROM emp;
-- 40 0 ( ์•”๊ธฐ ) [] OUTER JOIN
SELECT d.deptno
    , COUNT( e.empno ) ๋ถ€์„œ์›์ˆ˜
FROM emp  e, dept d  -- RIGHT OUTER JOIN
WHERE e.deptno(+) = d.deptno
GROUP BY d.deptno
ORDER BY d.deptno ASC;



-- JOIN ON ๊ตฌ๋ฌธ ์‚ฌ์šฉํ•ด์„œ ์ˆ˜์ • 

SELECT d.deptno
    , COUNT( e.empno ) ๋ถ€์„œ์›์ˆ˜
    , NVL( SUM( sal + NVL(comm,0)), 0) ์ด๊ธ‰์—ฌํ•ฉ
    , NVL( ROUND(  AVG( sal + NVL(comm,0)) , 2 ), 0) ํ‰๊ท 
--FROM emp  e JOIN  dept d  ON e.deptno(+) = d.deptno
--FROM emp  e RIGHT OUTER JOIN  dept d  ON e.deptno = d.deptno
FROM emp  e RIGHT JOIN  dept d  ON e.deptno = d.deptno
GROUP BY d.deptno
ORDER BY d.deptno ASC;



--10-1.  emp ํ…Œ์ด๋ธ”์—์„œ 30๋ฒˆ์ธ ๋ถ€์„œ์˜ ์ตœ๊ณ , ์ตœ์ € SAL์„ ์ถœ๋ ฅํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ.
--๊ฒฐ๊ณผ)
--  MIN(SAL)   MAX(SAL)
------------ ----------
--       950       2850

SELECT MAX( sal ) , MIN( sal )
FROM emp
WHERE deptno = 30;



--  ๊ฐ ๋ถ€์„œ๋ณ„ ์ตœ๊ณ , ์ตœ์ € sal ์ถœ๋ ฅ

SELECT deptno, MAX( sal ) , MIN( sal )
FROM emp
GROUP BY deptno
HAVING deptno = 30;     -- GROUP BY ์ ˆ์˜ ์กฐ๊ฑด์ ˆ์ด HAVING์ ˆ์ด๋‹ค.
--ORDER BY deptno;




--10-2.  emp ํ…Œ์ด๋ธ”์—์„œ 30๋ฒˆ์ธ ๋ถ€์„œ์˜ ์ตœ๊ณ , ์ตœ์ € SAL๋ฅผ ๋ฐ›๋Š” ์‚ฌ์›์˜ ์ •๋ณด ์ถœ๋ ฅํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ.
--
--๊ฒฐ๊ณผ)
--     EMPNO ENAME      HIREDATE        SAL
------------ ---------- -------- ----------
--      7698 BLAKE      81/05/01       2850
--      7900 JAMES      81/12/03        950    

-- ORA-00907: missing right parenthesis
--              ์˜ค๋ฅธ์ชฝ ๊ด„ํ˜ธ ๋ˆ„๋ฝ

SELECT EMPNO, ENAME,      HIREDATE ,       SAL
FROM emp
WHERE deptno = 30 AND sal IN (   
                                  (  SELECT MAX(sal) FROM emp WHERE deptno=30  )
                                , ( SELECT MIN(sal) FROM emp WHERE deptno=30   )
                              );


-- WHERE deptno = 30 AND sal IN ( 2850, 950 );

-- JOIN

SELECT empno, ename, hiredate ,sal
FROM ( 
        SELECT MAX(sal) max_sal, MIN(sal) min_sal
        FROM emp
        WHERE deptno = 30
) t , emp e
WHERE e.deptno = 30 AND e.sal = t.max_sal OR e.sal = t.min_sal;

 


--11.  insa ํ…Œ์ด๋ธ”์—์„œ 
--[์‹คํ–‰๊ฒฐ๊ณผ]
--๋ถ€์„œ๋ช…     ์ด์‚ฌ์›์ˆ˜ ๋ถ€์„œ์‚ฌ์›์ˆ˜ ์„ฑ๋ณ„  ์„ฑ๋ณ„์‚ฌ์›์ˆ˜  ๋ถ€/์ „%     ๋ถ€์„ฑ/์ „%     ์„ฑ/๋ถ€%
--๊ฐœ๋ฐœ๋ถ€       60           14            F           8          23.3%      13.3%       57.1%
--๊ฐœ๋ฐœ๋ถ€       60           14            M          6          23.3%      10%           42.9%
--๊ธฐํš๋ถ€       60            7             F           3          11.7%         5%           42.9%
--๊ธฐํš๋ถ€       60            7            M           4          11.7%        6.7%         57.1%
--์˜์—…๋ถ€       60           16            F           8          26.7%      13.3%        50%
--์˜์—…๋ถ€       60           16            M          8          26.7%      13.3%        50%
--์ธ์‚ฌ๋ถ€       60            4            M           4          6.7%         6.7%        100%
--์ž์žฌ๋ถ€       60            6             F           4          10%           6.7%        66.7%
--์ž์žฌ๋ถ€       60            6            M           2          10%           3.3%        33.3%
--์ด๋ฌด๋ถ€       60            7             F           3          11.7%         5%           42.9%
--์ด๋ฌด๋ถ€       60            7            M          4           11.7%         6.7%        57.1%
--ํ™๋ณด๋ถ€       60            6            F           3           10%           5%           50%
--ํ™๋ณด๋ถ€       60            6            M         3            10%           5%           50%             

 

SELECT t2.*
   , ROUND( ๋ถ€์„œ์‚ฌ์›์ˆ˜/์ด์‚ฌ์›์ˆ˜*100, 2) || '%' "๋ถ€/์ „%"
   , ROUND( "์„ฑ๋ณ„์‚ฌ์›์ˆ˜"/์ด์‚ฌ์›์ˆ˜*100, 2) || '%' "๋ถ€์„ฑ/์ „%"
   , ROUND( "์„ฑ๋ณ„์‚ฌ์›์ˆ˜"/๋ถ€์„œ์‚ฌ์›์ˆ˜*100, 2) || '%' "์„ฑ/๋ถ€%"
FROM (
            SELECT buseo
                 , gender
                 , (SELECT COUNT(*) FROM insa) ์ด์‚ฌ์›์ˆ˜
                 , (SELECT COUNT(*) FROM insa WHERE buseo = t.buseo ) ๋ถ€์„œ์‚ฌ์›์ˆ˜
                 , COUNT(*) "์„ฑ๋ณ„์‚ฌ์›์ˆ˜"
            FROM ( 
                SELECT buseo, name, ssn  
                      , DECODE(  MOD( SUBSTR( ssn, -7, 1), 2), 1 ,'M','F' )  gender
                FROM insa 
            ) t
            GROUP BY buseo , gender
            ORDER BY buseo , gender 
) t2;




--12. insaํ…Œ์ด๋ธ”์—์„œ ์—ฌ์ž์ธ์›์ˆ˜๊ฐ€ 5๋ช… ์ด์ƒ์ธ ๋ถ€์„œ๋งŒ ์ถœ๋ ฅ.  
-- ใ„ฑ. 

SELECT *
FROM( 
        SELECT buseo, COUNT(*) ์—ฌ์ž์‚ฌ์›์ˆ˜
        FROM insa
        WHERE MOD( SUBSTR( ssn, -7, 1), 2 ) = 0
        GROUP BY buseo 
) t
WHERE t.์—ฌ์ž์‚ฌ์›์ˆ˜ >= 5;


-- ใ„ด.              HAVING ์ ˆ : GROUP BY  ์กฐ๊ฑด์ ˆ

 5 SELECT buseo, COUNT(*) ์—ฌ์ž์‚ฌ์›์ˆ˜
 1 FROM insa
 2 WHERE MOD( SUBSTR( ssn, -7, 1), 2 ) = 0
 3 GROUP BY buseo
 4 HAVING COUNT(*) >= 5;

 

 

--13. insa ํ…Œ์ด๋ธ”์—์„œ ๊ธ‰์—ฌ(pay= basicpay+sudang)๊ฐ€ ์ƒ์œ„ 15%์— ํ•ด๋‹น๋˜๋Š” ์‚ฌ์›๋“ค ์ •๋ณด ์ถœ๋ ฅ 

SELECT *
FROM (
            SELECT buseo, name,  basicpay+sudang pay
               , RANK() OVER( ORDER BY basicpay+sudang DESC )  pay_rank
            FROm insa
) t
WHERE t.pay_rank <= (SELECT COUNT(*) FROM insa)*0.15;
WHERE t.pay_rank <= 9;



--14. emp ํ…Œ์ด๋ธ”์—์„œ sal์˜ ์ „์ฒด์‚ฌ์›์—์„œ์˜ ๋“ฑ์ˆ˜ , ๋ถ€์„œ๋‚ด์—์„œ์˜ ๋“ฑ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ฟผ๋ฆฌ ์ž‘์„ฑ
        -- ROW_NUMBER()

        SELECT deptno,  empno, ename, sal
              , RANK() OVER(ORDER BY sal DESC)  w_rank   -- ์ „๊ต๋“ฑ์ˆ˜
              , RANK() OVER( PARTITION BY deptno ORDER BY sal DESC)  b_rank  -- ๋ฐ˜๋“ฑ์ˆ˜
        FROM emp
        ORDER BY deptno, b_rank ASC;

        
-- ์•ž ๋’ค์— ์žˆ๋Š” ํŠน์ • ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ํ•จ์ˆ˜ 

SELECT '    ADMIN    '        -- JAVA : trim()
     -- , REPLACE( '    ADMIN    ' , ' ', '' )
     -- , TRIM( ํŠน์ •๋ฌธ์ž  FROM ๋ฌธ์ž์—ด)
     , TRIM( ' '  FROM '    ADMIN    ') 
     , TRIM( '*'  FROM '****AD*MIN****') 
FROM dual;


-- GROUP BY + HAVING ์ ˆ --
1) 

SELECT 
   COUNT(*)
   , COUNT(   DECODE(deptno, 10, 'O')  )
   , COUNT(   DECODE(deptno, 20, 'O')  )
   , COUNT(   DECODE(deptno, 30, 'O')  )
   , COUNT(   DECODE(deptno, 40, 'O')  )
FROM emp;


2)

SELECT d.deptno , COUNT(e.empno)
FROM emp e, dept d 
WHERE e.deptno(+) = d.deptno
GROUP BY d.deptno
ORDER BY d.deptno ASC;



 ( ๋ฌธ์ œ ) emp ํ…Œ์ด๋ธ”์—์„œ  20, 40๋ฒˆ ๋ถ€์„œ์›์€ ์ œ์™ธํ•˜๊ณ 
         ๊ทธ ์™ธ ๋ถ€์„œ์˜ ์‚ฌ์›์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ 
         ๊ทธ ๋ถ€์„œ์˜ ์‚ฌ์›์ˆ˜๊ฐ€ 4๋ช… ์ด์ƒ์ธ ๋ถ€์„œ์ •๋ณด๋ฅผ ์ถœ๋ ฅ.
         -- FROM (

SELECT deptno, COUNT(*)
FROM emp
WHERE deptno NOT IN ( 20, 40 )
GROUP BY deptno
HAVING COUNT(*) >= 4;


  ( ์ถ”๊ฐ€๋ฌธ์ œ )       deptno, [ dname ], COUNT(*)  ๋ถ€์„œ๋ช…๋„ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜๊ณ ์ž ํ•œ๋‹ค.  *****
           dept : dname , dpetno
           emp :  deptno
           
           1) ์กฐ์ธ
       --  ORA-00979: not a GROUP BY expression *****
       -- GROUP BY ์ ˆ  + ์ง‘๊ณ„ํ•จ์ˆ˜

         SELECT d.deptno, dname,  COUNT(*)
         FROM emp e JOIN dept d ON e.deptno = d.deptno
         WHERE d.deptno NOT IN ( 20, 40 )
         GROUP BY d.deptno , dname
         HAVING COUNT(*) >= 4;


  (๋ฌธ์ œ) insaํ…Œ์ด๋ธ”์—์„œ ๊ฐ ๋ถ€์„œ๋ณ„๋กœ          ๊ณผ์žฅ ๋ช‡๋ช…, ๋Œ€๋ฆฌ ๋ช‡๋ช…, ์‚ฌ์› ๋ช‡๋ช… ๋“ฑ๋“ฑ ์ง๊ธ‰๋ณ„ ์‚ฌ์›์ˆ˜๋ฅผ ์กฐํšŒ.

  SELECT buseo, jikwi, COUNT(*) ์‚ฌ์›์ˆ˜
  FROM insa
  GROUP BY buseo , jikwi
  ORDER BY buseo ASC, jikwi ASC;


  
  (์ถ”๊ฐ€๋ฌธ์ œ) ์–ด๋–ค ๋ถ€์„œ์˜ ์‚ฌ์›์ด ์—†๋Š” ์ง์œ„๋ช…์ด๋ผ๋„      ๊ณผ์ •  0  ์œผ๋กœ ์ถœ๋ ฅ.
  SELECT DISTINCT jikwi
  FROM insa;


    ๊ณผ์žฅ
    ๋Œ€๋ฆฌ
    ๋ถ€์žฅ
    ์‚ฌ์›
   
 --  [ ROLLUP  ๊ณผ  CUBE   ์„ค๋ช… ] ์ •๋ฆฌ : https://for-my-wealthy-life.tistory.com/44
 
 -- (๋ฌธ์ œ) insa ํ…Œ์ด๋ธ”์—์„œ ๋‚จ์ž์‚ฌ์›์ˆ˜,์—ฌ์ž์‚ฌ์›์ˆ˜๋ฅผ ์กฐํšŒ + ์ด์‚ฌ์›์ˆ˜ ์กฐํšŒ

 SELECT DECODE(  MOD(  SUBSTR( ssn, -7, 1) , 2 ) , 1 , '๋‚จ์ž', '์—ฌ์ž') gender
       , COUNT(*)
 FROM insa
 GROUP BY   MOD(  SUBSTR( ssn, -7, 1) , 2 ) 
 UNION ALL -- ์ปฌ๋Ÿผ๊ฐฏ์ˆ˜,
 SELECT ' ' , COUNT(*)
 FROM insa;


 -- ์œ„์˜ ์ฟผ๋ฆฌ๋ฅผ  ROLLUP ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ...
 ROLLUP์€ GROUP BY ์ ˆ์˜ ๊ทธ๋ฃน ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ „์ฒด ํ–‰์„ ๊ทธ๋ฃนํ™” ํ•˜๊ณ , ๊ฐ ๊ทธ๋ฃน์— ๋Œ€ํ•ด ๋ถ€๋ถ„ํ•ฉ์„ ๊ตฌํ•˜๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค. 
 CUBE๋Š” ROLLUP์— ์˜ํ•œ ๊ทธ๋ฃน ๊ฒฐ๊ณผ์™€ GROUP BY ์ ˆ์— ๊ธฐ์ˆ ๋œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๊ทธ๋ฃน ์กฐํ•ฉ์„ ๋งŒ๋“œ๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค. ์ฆ‰, ROLLUP ์—ฐ์‚ฐ์ž๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ์— ๋”ํ•ด GROUP BY ์ ˆ์— ๊ธฐ์ˆ ๋œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ๊ทธ๋ฃนํ•‘ ์กฐํ•ฉ์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

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

        SELECT   ์ปฌ๋Ÿผ๋ช…, ๊ทธ๋ฃนํ•จ์ˆ˜(์ปฌ๋Ÿผ๋ช…)
        FROM     ํ…Œ์ด๋ธ”๋ช…
        WHERE    ์กฐ๊ฑด
        GROUP BY [ROLLUP ¦ CUBE]๊ทธ๋ฃนํ•‘ํ•˜๊ณ ์žํ•˜๋Š” ์ปฌ๋Ÿผ๋ช…,...
        HAVING   ๊ทธ๋ฃน์กฐ๊ฑด
        ORDER BY ์ปฌ๋Ÿผ๋ช… ๋˜๋Š” ์œ„์น˜๋ฒˆํ˜ธ



select ๋ฌธ์˜ prototype ์ฐธ์กฐ
• ROLLUP๊ณผ CUBE๋Š” GROUP BY ์ ˆ ๋’ค์— ๊ธฐ์ˆ ํ•œ ์ปฌ๋Ÿผ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ์ถœ๋ ฅ๋˜๋Š” ๊ฒฐ๊ณผ ์…‹์ด ๋‹ฌ๋ผ์ง„๋‹ค.
• GROUP BY ๋’ค์— ๊ธฐ์ˆ ํ•œ ์ปฌ๋Ÿผ์ด 2๊ฐœ์ผ ๊ฒฝ์šฐ ROLLUP์€ n+1์—์„œ 3๊ฐœ์˜ ๊ทธ๋ฃน๋ณ„ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ , CUBE๋Š” 2*n์—์„œ 2*2=4๊ฐœ์˜ ๊ฒฐ๊ณผ ์…‹์ด ์ถœ๋ ฅ๋œ๋‹ค.

 ex)

SELECT DECODE(  MOD(  SUBSTR( ssn, -7, 1) , 2 ) , 1 , '๋‚จ์ž', 0, '์—ฌ์ž', 'ํ•ฉ') gender
       , COUNT(*)
 FROM insa
 GROUP BY  ROLLUP (  MOD(  SUBSTR( ssn, -7, 1) , 2 ) );



--(๋ฌธ์ œ)  insa ํ…Œ์ด๋ธ”์—์„œ
--       1์ฐจ ๊ทธ๋ฃนํ™” : ๋ถ€์„œ๋ณ„ ,            2์ฐจ ๊ทธ๋ฃนํ™” : ์ง๊ธ‰๋ณ„ 
--       COUNT(*), SUM(*), AVG(*) ๋“ฑ๋“ฑ ์ง‘๊ณ„ํ•จ์ˆ˜...

  -- ORA-00933: SQL command not properly ended
  --            SQL ๋ช…๋ น์–ด๊ฐ€ ์ œ๋Œ€๋กœ ์ข…๋ฃŒ๋˜์ง€ ์•Š์•˜๋‹ค.

  SELECT buseo, jikwi, COUNT(*) ์‚ฌ์›์ˆ˜
  FROM insa
  GROUP BY buseo, jikwi
  --ORDER BY buseo, jikwi
  UNION ALL
  SELECT buseo, '' jikwi,  COUNT(*) 
  FROM insa
  GROUP BY buseo
--   UNION ALL
--  SELECT '',  jikwi,  COUNT(*) 
--  FROM insa
--  GROUP BY jikwi
  UNION ALL
  SELECT '' buseo, '' jikwi,  COUNT(*) 
  FROM insa 
  ORDER BY buseo , jikwi;


  --     

  SELECT buseo, jikwi, COUNT(*) ์‚ฌ์›์ˆ˜
  FROM insa
  -- GROUP BY  ROLLUP( buseo, jikwi )
  GROUP BY  CUBE( buseo, jikwi )
  ORDER BY buseo, jikwi   ;

 

  
GROUP BY    buseo, jikwi     ๋ถ€์„œ๋ณ„ / ์ง์œ„      ์‚ฌ์›์ˆ˜
GROUP BY   ROLLUP(  buseo, jikwi )     ๋ถ€์„œ๋ณ„ / ์ง์œ„      ์‚ฌ์›์ˆ˜
                                      ๋ถ€์„œ๋ณ„                ์ด์‚ฌ์›์ˆ˜ ( ๋ถ€๋ถ„ํ•ฉ )
                                                        ์ „์ฒด ์‚ฌ์›์ˆ˜ ( ๋ถ€๋ถ„ํ•ฉ )
  
GROUP BY   CUBE(  buseo, jikwi )     ๋ถ€์„œ๋ณ„ / ์ง์œ„      ์‚ฌ์›์ˆ˜
                                      ๋ถ€์„œ๋ณ„                ์ด์‚ฌ์›์ˆ˜ ( ๋ถ€๋ถ„ํ•ฉ )
                                             ์ง์œ„๋ณ„       ์ด์‚ฌ์›์ˆ˜( ๋ถ€๋ถ„ํ•ฉ )
                                                        ์ „์ฒด ์‚ฌ์›์ˆ˜ ( ๋ถ€๋ถ„ํ•ฉ )


  
  
  
        
        
        
        
      
      

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