[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 ) ๋ถ์๋ณ / ์ง์ ์ฌ์์
๋ถ์๋ณ ์ด์ฌ์์ ( ๋ถ๋ถํฉ )
์ง์๋ณ ์ด์ฌ์์( ๋ถ๋ถํฉ )
์ ์ฒด ์ฌ์์ ( ๋ถ๋ถํฉ )
์ต๊ทผ๋๊ธ