๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–‹๏ธ ๊ธฐ์ˆ ์‹ฌํ™”/SQLD

[SQLD๋ฒผ๋ฝ์น˜๊ธฐ] ์ถ”๊ฐ€ ์˜ค๋‹ต๋…ธํŠธ

by OR15A 2023. 11. 17.

ISLEAF = 1 : ๋‚ด๊ฐ€ ์ฝ์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž์‹์ด ์—†์œผ๋ฉด 1, ์žˆ์œผ๋ฉด 0

SELECT A, B, AVG(SAL) OVER (  PARTITIONA BY DEPT
                                                       ORDER BY SAL
                                                       BETWEEN 1 PRECEDING
                                                        AND 1 FLLOWING ) AS C1
FROM your_table;                                                     
  • SELECT A, B: A์™€ B๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์ปฌ๋Ÿผ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
  • AVG(SAL) OVER (...) AS C1: SAL ์ปฌ๋Ÿผ์˜ ๊ฐ’์— ๋”ฐ๋ผ ์ •๋ ฌ๋œ ์œˆ๋„์šฐ(๋ฐ์ดํ„ฐ์˜ ํ•˜์œ„ ์ง‘ํ•ฉ) ๋‚ด์—์„œ ํ‰๊ท  ๊ธ‰์—ฌ๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ์ด ๊ฒฐ๊ณผ๋ฅผ C1์ด๋ผ๋Š” ๋ณ„์นญ์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
  • PARTITIONA BY DEPT : ๊ฐ DEPT (๋ถ€์„œ)๋Š” ๋ณ„๋„์˜ ํŒŒํ‹ฐ์…˜์œผ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
  • ORDER BY SAL: ์œˆ๋„์šฐ๋ฅผ SAL ์ปฌ๋Ÿผ์˜ ๊ฐ’์— ๋”ฐ๋ผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  • BETWEEN 1 PRECEDING AND 1 FOLLOWING: ํ˜„์žฌ ํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ”๋กœ ์•ž(1 preceding)๊ณผ ๋ฐ”๋กœ ๋’ค(1 following) ํ–‰์„ ํฌํ•จํ•œ ์œˆ๋„์šฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ๊ฐ ํ–‰์— ๋Œ€ํ•ด ํ•ด๋‹น ํ–‰, ๊ทธ ์•ž์˜ ํ–‰, ๊ทธ๋ฆฌ๊ณ  ๋’ค์˜ ํ–‰์„ ํฌํ•จํ•˜์—ฌ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
SELECT A, B, C, GROUPING_ID(A, B, C), SUM(value)
FROM your_table
GROUP BY ROLLUP(A, B, C);

GROUPING_ID(A, B, C)

(2^2, 2^1, 2^0)  (4,2,1)

A๋งŒ ์ฐธ์—ฌ : 2+1 = 3

A, B ์ฐธ์—ฌ : 1

A, B, C ์ฐธ์—ฌ X : 4+2+1 = 7

DECADE(col1, 'A', col2, 'B',  col3, 'C', 'Z' ) : col1์ด๋ฉด A, col2์ด๋ฉด B,  col3์ด๋ฉด C, ํ•ด๋‹น ์—†์œผ๋ฉด Z

ORGER BY  ์„ ์ˆ˜๋ช… ASC, ํŒ€๋ช…, 3 DESC : ์„ ์ˆ˜๋ช…๊ณผ ํŒ€๋ช…์€ ์˜ค๋ฆ„์ฐจ์ˆœ, ์—ฐ๋ด‰์€ ๋‚ด๋ฆผ์ฐจ์ˆœ

SELECT์ ˆ์— ์—†๋Š” ์ปฌ๋Ÿผ์€ 1 2 ๊ฐ™์€ ์ •์ˆ˜ ์ž๋ฆฌ์ˆ˜๋กœ ORDER BY ์•ˆ๋จ

NTILE(4)  10๊ฐœ๋ฅผ 4๊ฐœ๋กœ ๋‚˜๋ˆŒ ๋•Œ  3 3 2 2 ๋กœ ๋‚˜๋‰จ

NTILE(3)  5๊ฐœ๋ฅผ 3๊ฐœ๋กœ ๋‚˜๋ˆŒ ๋•Œ  2 2 1 ๋กœ ๋‚˜๋‰จ