[STARTERS 4๊ธฐ TIL] #10์ผ์ฐจ(23.02.17)

 

๐Ÿ“š ์˜ค๋Š˜ ๋‚ด๊ฐ€ ๋ฐฐ์šด ๊ฒƒ

๐Ÿ“ ์ž์ฃผ ์“ฐ์ด๋Š” ๋‹จ์ผ ํ–‰ ํ•จ์ˆ˜(1) 

  • ๋ฌธ์ž ํ•จ์ˆ˜
LOWER (char) char์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ๋ฆฌํ„ด
UPPER (char) char์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ๋ฆฌํ„ด
CONCAT (char1, char2) char1์˜ ๋ฌธ์ž์—ด๊ณผ char2์˜ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฆฌํ„ด
SELECT CONCAT ('ABC', 'def') AS CONCAT_FN
  FROM dual;

 

SUBSTR (char, position [, length]) char ๋ฌธ์ž์—ด์˜ position ์œ„์น˜๋กœ๋ถ€ํ„ฐ length๊ฐœ ๋ฌธ์ž๋ฅผ ์ž˜๋ผ๋‚ด์„œ ๋ฆฌํ„ด
REPLACE (char, search_string [, replace_string]) char ๋ฌธ์ž์—ด์—์„œ search_string ๊ฐ’์„ replace_string ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•˜์—ฌ ๋ฆฌํ„ด
SELECT SUBSTR ('ABCDEFGH', 4, 2)          AS SUBSTR_FN
     , REPLACE ('ABCDEFGH', 'DEF', 'ZZZ') AS REPLACE_FN1
     , REPLACE ('ABCDEFGH', 'DEF')        AS REPLACE_FN2
  FROM dual;

[๊ฒฐ๊ณผ]

SUBSTR_FN REPLACE_FN1 REPLACE_FN2
DE ABCZZZGH ABCGH

 

LPAD (expr1, n [, expr2]) expr1์„ n์ž๋ฆฌ๋งŒํผ ๋Š˜๋ฆฌ๊ณ , ์™ผ์ชฝ ๋นˆ ๊ณต๊ฐ„์„ expr2๋กœ ์ฑ„์›Œ์„œ ๋ฆฌํ„ด
RPAD (expr1, n [, expr2]) expr1์„ n์ž๋ฆฌ๋งŒํผ ๋Š˜๋ฆฌ๊ณ , ์˜ค๋ฅธ์ชฝ ๋นˆ ๊ณต๊ฐ„์„ expr2๋กœ ์ฑ„์›Œ์„œ ๋ฆฌํ„ด
SELECT LPAD ('ABC', 8, 'Z')          AS LPAD_FN1
     , LPAD ('ABC', 8)               AS LPAD_FN2
     , RPAD ('ABC', 8, '12')          AS RPAD_FN1
  FROM dual;

[๊ฒฐ๊ณผ]

LPAD_FN1 LPAD_FN2 RPAD_FN1
ZZZZZABC ABC ABC12121

 

LTRIM (char [, set]) char ๋ฌธ์ž์—ด์˜ ์™ผ์ชฝ๋ถ€ํ„ฐ set ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ๋ฆฌํ„ด (set ๊ธฐ๋ณธ ๊ฐ’์€ ๊ณต๋ฐฑ)
RTRIM (char [, set]) char ๋ฌธ์ž์—ด์˜ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ set ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ๋ฆฌํ„ด (set ๊ธฐ๋ณธ ๊ฐ’์€ ๊ณต๋ฐฑ)
TRIM ([LEADING TRAILING

 

LENGTH (char) char ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฆฌํ„ด
INSTR (char, search_string [, position [, occurrence]]) char ๋ฌธ์ž์—ด์˜ position ์œ„์น˜๋กœ๋ถ€ํ„ฐ occurrence ๋ฒˆ์งธ๋กœ ์ฐพ์€ search_string์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ๋ฆฌํ„ด
SELECT INSTR ('ABCABCAB', 'AB', 3, 2)     AS LENGTH_FN1
	   , INSTR ('ABCABCAB', 'AB', 3)        AS LENGTH_FN2
		 , INSTR ('ABCABCAB', 'AB')           AS LENGTH_FN3
		 , INSTR ('ABCABCAB', 'AB', 3, 3)     AS LENGTH_FN4
  FROM dual;

[๊ฒฐ๊ณผ]

LENGTH_FN1 LENGTH_FN2 LENGTH_FN3 LENGTH_FN4
7 4 1 0
  • ์ˆซ์ž ํ•จ์ˆ˜
ABS (n) ์ˆซ์ž n์˜ ์ ˆ๋Œ€ ๊ฐ’์„ ๋ฆฌํ„ด
SIGN (n) ์ˆซ์ž n์˜ ๋ถ€ํ˜ธ๋ฅผ ๋ฆฌํ„ด (n์ด ์–‘์ˆ˜๋ฉด 1, ์Œ์ˆ˜๋ฉด -1, 0์ด๋ฉด 0 ์„ ๋ฆฌํ„ด)

 

ROUND (n1 [, n2]) ์ˆซ์ž n1์„ ์†Œ์ˆ˜์  n2 ์งธ ์ž๋ฆฌ๋กœ ‘๋ฐ˜์˜ฌ๋ฆผ’ํ•˜์—ฌ ๋ฆฌํ„ด (n2๊ฐ€ 0์ด๊ฑฐ๋‚˜ ์ƒ๋žต๋˜๋ฉด ์ •์ˆ˜)
TRUNC (n1 [, n2]) ์ˆซ์ž n1์„ ์†Œ์ˆ˜์  n2 ์งธ ์ž๋ฆฌ๋กœ ‘๋ฒ„๋ฆผ’ํ•˜์—ฌ ๋ฆฌํ„ด (n2๊ฐ€ 0์ด๊ฑฐ๋‚˜ ์ƒ๋žต๋˜๋ฉด ์ •์ˆ˜)

 

ROUND (n1 [, n2]) ์ˆซ์ž n๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ์ตœ์†Œ์˜ ์ •์ˆ˜ ๊ฐ’์„ ๋ฆฌํ„ด
FLOOR (n) ์ˆซ์ž n๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ตœ๋Œ€์˜ ์ •์ˆ˜ ๊ฐ’์„ ๋ฆฌํ„ด

 

MOD (n1, n2) ์ˆซ์ž n1์„ n2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ ๊ฐ’์„ ๋ฆฌํ„ด
POWER (n1, n2) ์ˆซ์ž n1์˜ n2 ์ œ๊ณฑ ๊ฐ’์„ ๋ฆฌํ„ด
SQRT (n) ์ˆซ์ž n์˜ ์ œ๊ณฑ๊ทผ ๊ฐ’์„ ๋ฆฌํ„ด
  • ๋‚ ์งœ ํ•จ์ˆ˜
SYSDATE ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„œ๋ฒ„์˜ ํ˜„์žฌ ๋‚ ์งœ ๊ฐ’์„ ๋ฆฌํ„ด
ROUND (date [, fmt]) ๋‚ ์งœ date๋ฅผ fat ํฌ๋งท ์š”์†Œ ๊ธฐ์ค€์œผ๋กœ ‘๋ฐ˜์˜ฌ๋ฆผ’ํ•˜์—ฌ ๋ฆฌํ„ด
(fmt: YY, MM, DD, HH, MI ๋“ฑ)
TRUNC (date [, fmt]) ๋‚ ์งœ date๋ฅผ fmt ํฌ๋งท ์š”์†Œ ๊ธฐ์ค€์œผ๋กœ ‘๋ฒ„๋ฆผ’ํ•˜์—ฌ ๋ฆฌํ„ด

 

 NEXT_DAY (date, n) ๋‚ ์งœ date ์ดํ›„ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด n ์š”์ผ ๋‚ ์งœ ๊ฐ’์„ ๋ฆฌํ„ด
1: ์ผ, 2: ์›”, 3: ํ™”, 4: ์ˆ˜, 5: ๋ชฉ, 6: ๊ธˆ, 7: ํ†  
ROUND (date [, fmt]) ๋‚ ์งœ date๊ฐ€ ์†ํ•œ ์›”์˜ ๋งˆ์ง€๋ง‰ ๋‚ ์งœ ๊ฐ’์„ ๋ฆฌํ„ด 

 

ADD_MONTHS (date, n) ๋‚ ์งœ date์— n ๊ฐœ์›” ์ˆ˜๋ฅผ ๋”ํ•œ ๋‚ ์งœ ๊ฐ’์„ ๋ฆฌํ„ด
MONTHS_BETWEEN (date1, date2) ๋‘ ๋‚ ์งœ (date1, date2) ๊ฐ„์˜ ๊ฐœ์›” ์ˆ˜๋ฅผ ๋ฆฌํ„ด

 

 

๐Ÿ“ GROUP BY ์ ˆ 

SELECT ์นผ๋Ÿผ, ์นผ๋Ÿผ, ...
  FROM ํ…Œ์ด๋ธ” a, ํ…Œ์ด๋ธ” b, ...
 WHERE ์กฐ๊ฑด 
GROUP BY ์นผ๋Ÿผ, ์นผ๋Ÿผ;
SELECT a.job, SUM(a.sal), MAX(a.sal), MIN(a.sal)
  FROM emp a
GROUP BY a.job;
  • SELECT ๋ฌธ์— GROUP BY ์ ˆ์ด ํฌํ•จ๋˜์–ด ์žˆ์„ ๋•Œ,
  • **๊ธฐ์ค€ ์นผ๋Ÿผ ์™ธ ๋‹ค๋ฅธ ์นผ๋Ÿผ**์„ SELECT ์ ˆ์— ํฌํ•จ์‹œํ‚ค๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ !
SELECT a.job, a.sal
  FROM emp a
GROUP BY a.job;
  • ์ง‘๊ณ„ ํ•จ์ˆ˜

      - ์ง‘๊ณ„ ํ•จ์ˆ˜๋Š” ๋‹ค์ค‘ ํ–‰(ํ–‰ ๊ทธ๋ฃน)์˜ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์•„ ๋‹จ์ผ ํ–‰์˜ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜

      - COUNT, SUM, AVG, MAX, MIN

  • ์ง‘๊ณ„ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ํ–‰ ๊ทธ๋ฃน ๋ณ„๋กœ NULL์„ ์ œ์™ธํ•œ ๊ฐ’์ด ์ง‘๊ณ„๋จ
  • ์ง‘๊ณ„ํ•จ์ˆ˜์˜ ์ธ์ž์— ์—ฐ์‚ฐ์ด๋‚˜ ๊ฐ€๊ณต์ด ์กด์žฌํ•˜๋ฉด, ์ด๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜ ์‹คํ–‰ ์ „์— ํ–‰ ๊ทธ๋ฃน ๋‚ด์— ์žˆ๋Š” ๊ฐœ๋ณ„ ํ–‰ ๋‹จ์œ„๋กœ ์ˆ˜ํ–‰๋จ
SELECT a.deptno
	   , COUNT(NVL(a.comm, 0) AS CNT
		 , SUM(NVL(a.comm, 0) AS SUM
		 , AVG(NVL(a.comm, 0) AS AVG
  FROM emp a
GROUP BY a.deptno;

 

  • GROUP BY ์ ˆ ์—†์ด SELECT ์ ˆ์— ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์ „์ฒด ํ–‰์ด ํ•˜๋‚˜์˜ ๊ทธ๋ฃน ํ–‰์ด ๋œ๋‹ค.

 

 

๐Ÿ“ ์ง‘๊ณ„ํ•จ์ˆ˜ 

COUNT(*) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ ์ „์ฒด ํ–‰์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ด
COUNT(expr) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ NULL์ด ์•„๋‹Œ expr(ํ‘œํ˜„์‹)์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ด

 

SUM(expr) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ NULL์ด ์•„๋‹Œ expr(ํ‘œํ˜„์‹)์˜ ํ•ฉ๊ณ„ ๊ฐ’ ๋ฆฌํ„ด
AVG(expr) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ NULL์ด ์•„๋‹Œ expr(ํ‘œํ˜„์‹)์˜ ํ‰๊ท  ๊ฐ’ ๋ฆฌํ„ด
SELECT a.deptno
		 , SUM(a.comm)
		 , AVG(a.comm)
		 , NVL(AVG(a.comm),0)     //ํ‰๊ท  ๊ฐ’์„ ๋‚ด๊ณ  NULL ๊ฐ’์„ 0์œผ๋กœ ์น˜ํ™˜
		 , AVG(NVL(a.comm, 0))    // NULL ๊ฐ’์„ 0์œผ๋กœ ์น˜ํ™˜ํ•œ ๋’ค ํ‰๊ท  ๊ฐ’ ๋‚ด๊ธฐ
FROM emp a
GROUP BY a.deptno;

 

MAX(expr) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ NULL์ด ์•„๋‹Œ expr(ํ‘œํ˜„์‹)์˜ ์ตœ๋Œ€ ๊ฐ’ ๋ฆฌํ„ด
MIN(expr) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ NULL์ด ์•„๋‹Œ expr(ํ‘œํ˜„์‹)์˜ ์ตœ์†Œ ๊ฐ’ ๋ฆฌํ„ด

 

VARIANCE(expr) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ NULL์ด ์•„๋‹Œ expr(ํ‘œํ˜„์‹)์˜ ๋ถ„์‚ฐ ๊ฐ’ ๋ฆฌํ„ด
STDDEV(expr) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ NULL์ด ์•„๋‹Œ expr(ํ‘œํ˜„์‹)์˜ ํ‘œ์ค€ํŽธ์ฐจ ๊ฐ’ ๋ฆฌํ„ด

 

STATS_MODE(expr) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ NULL์ด ์•„๋‹Œ expr(ํ‘œํ˜„์‹)์˜ ์ตœ๋นˆ ๊ฐ’ ๋ฆฌํ„ด
MEDIAN(expr) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ NULL์ด ์•„๋‹Œ expr(ํ‘œํ˜„์‹)์˜ ์ค‘์•™ ๊ฐ’ ๋ฆฌํ„ด

 

LISTAGG(exp, ‘delimiter’) WITHIN GROUP (order_by_daue) ํ–‰ ๊ทธ๋ฃน์— ์†ํ•œ expr(ํ‘œํ˜„์‹)์„ delimiter ๋ฌธ์ž๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์—ฐ๊ฒฐํ•œ ๊ฐ’์„ ๋ฆฌํ„ด
SELECT a.deptno
		 , LISTAGG(a.ename, ',') WITHIN GROUP (ORDER BY a.sal DESC)
		 , LISTAGG(a.job, '/') WITHIN GROUP (ORDER BY a.job)
  FROM emp a
GROUP BY a.deptno;
  • CASE ํ‘œํ˜„์‹๊ณผ ์ง‘๊ณ„ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ๊ฑด๋งŒ ์ง‘๊ณ„(๋ถ€๋ถ„ ์ง‘๊ณ„)ํ•  ์ˆ˜ ์žˆ๋‹ค.
SELECT SUM(CASE WHEN a.deptno = 10
								THEN a.sal
					  END) AS SUMSAL_DEPT10
  FROM emp a;

 

 

๐Ÿ“ HAVING ์ ˆ

  • ์กฐํšŒ๋˜๋Š” ํ–‰ ๊ทธ๋ฃน์„ ์ œํ•œํ•  ์ˆ˜ ์žˆ๋‹ค.
SELECT ์นผ๋Ÿผ, ์นผ๋Ÿผ, ...
  FROM ํ…Œ์ด๋ธ”
 WHERE ์กฐ๊ฑด
GROUP BY ์นผ๋Ÿผ, ์นผ๋Ÿผ, ...
HAVING **์กฐ๊ฑด** ;
SELECT a.deptno, a.job, COUNT(*)
  FROM emp a
GROUP BY a.deptno, a.job
	HAVING COUNT(*) >= 2;
  • HAVING ์ ˆ์— ์ง‘๊ณ„ ํ•จ์ˆ˜ ์—†์ด ์กฐ๊ฑด์„ ๊ธฐ์ˆ ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ !
    • Grouping ์นผ๋Ÿผ์— ๋Œ€ํ•œ ์กฐ๊ฑด์€ ์ง‘๊ณ„ ํ•จ์ˆ˜๊ฐ€ ์—†๋”๋ผ๋„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง„ ์•Š์Œ
    • ํ•˜์ง€๋งŒ, ์„ฑ๋Šฅ ์ƒ WHERE ์ ˆ์— ๊ธฐ์ˆ ํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•จ
    • ์ง‘๊ณ„ ๊ฐ’์— ๋Œ€ํ•œ ์กฐ๊ฑด์€ HAVING ์ ˆ์—๋งŒ ๊ธฐ์ˆ  ๊ฐ€๋Šฅ (WHERE ์ ˆ ๋ถˆ๊ฐ€ !)
  • WHERE ์ ˆ์€ ํ–‰ ๊ทธ๋ฃน์ด ์ƒ์„ฑ๋˜๊ธฐ ์ „์— ์กฐ๊ฑด์— ๋”ฐ๋ผ ํ–‰์„ ์ œํ•œํ•˜๊ณ ,
  • HAVING ์ ˆ์€ ํ–‰ ๊ทธ๋ฃน์ด ์ƒ์„ฑ๋œ ํ›„์— ์กฐ๊ฑด์— ๋”ฐ๋ผ ํ–‰ ๊ทธ๋ฃน์„ ์ œํ•œํ•จ.

 

  • HAVING ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต ๊ฐ’ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธ(๋ฐ์ดํ„ฐ ๊ฒ€์ฆ)ํ•  ์ˆ˜ ์žˆ๋‹ค.
SELECT a.sal
		 , COUNT(*)
  FROM emp a
GROUP BY a.sal
	HAVING COUNT(*) > 1;

๐Ÿ’ญ ์˜ค๋Š˜ ๊ธฐ์–ตํ•ด์•ผ ํ•  ๋‚ด์šฉ

โ‘  ๋‹ค์–‘ํ•œ ๋‹จ์ผํ–‰ ํ•จ์ˆ˜

 ๋‹จ์ผํ–‰ ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‘ ์™ธ์šธ ํ•„์š”๋Š” ์—†์ง€๋งŒ, ๊ธฐ๋ณธ์ ์ธ ๊ฒƒ๋“ค์€ ๊ตฌ๊ธ€๋ง ์—†์ด๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ˆ™์ง€ํ•˜๊ณ  ์žˆ์–ด์•ผ๊ฒ ๋‹ค !

 

โ‘ก groupby์ ˆ๊ณผ having์ ˆ

์˜ˆ์ œ๋ฅผ ํ’€๋‹ค๋ณด๋ฉด having์ ˆ ๋Œ€์‹  where์ ˆ์„ ์‚ฌ์šฉํ•ด ํ‹€๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

๊ทธ๋Ÿฌ๋‹ˆ having์ ˆ์˜ ์‚ฌ์šฉ์ƒํ™ฉ์— ๋Œ€ํ•ด ์œ ๋…ํ•ด์•ผ ํ•œ๋‹ค !!

 

 

 

โœ๐Ÿป ํšŒ๊ณ 

์˜ค๋Š˜์€ SQL ๋งˆ์ง€๋ง‰ ์‹œ๊ฐ„์ด์—ˆ๋‹ค!

SQL์€ ๊ฐ•์˜๋ฅผ ๋“ฃ์„ ๋•Œ ์–ด๋ ค์›€์ด ์œ ๋… ๋งŽ์•˜๋‹ค.

๋ฐฐ์› ๋˜ ํ•จ์ˆ˜๋“ค์€ ์˜ˆ์ œ๋ฅผ ํ’€๋ฉด์„œ ๊ณ„์† ๋ณต์Šตํ•ด๋ณด๊ณ ,

๋‹ค์Œ ์˜คํ”„๋ผ์ธ SQL ๊ฐ•์˜์—์„œ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์„ ๋ณด๊ฐ•ํ•ด์•ผ๊ฒ ๋‹ค !!

 

๋‚ด์ผ์€ R ๊ฐ•์˜๊ฐ€ ์‹œ์ž‘๋œ๋‹ค !

๋‚ด์ผ๋„ ํ™”์ดํŒ… !!!๐Ÿคฉ