๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DB/Oracle

[Oracle] ROLLUP ์ดํ•ฉ๊ณ„ ๊ตฌํ•˜๊ธฐ

by yunamom 2022. 3. 25.
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” :D yunamom ์ž…๋‹ˆ๋‹ค.

 

Oracle ROLLUP ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํฌ์ŠคํŒ… ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜Š

oracle

๐Ÿ‘‡์•„๋ž˜๋Š” ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š” ํ™”๋ฉด์ž…๋‹ˆ๋‹ค.

๋ณ‘์›์ง€์—ญ ๋ณ‘์›์ง€์—ญ๋ช… ์ ‘์ข…์˜ˆ์•ฝ๊ฑด์ˆ˜
10 ์„œ์šธ 5
20 ๋Œ€์ „ 3
30 ๋Œ€๊ตฌ 2
40 ๊ด‘์ฃผ 0
์ดํ•ฉ 10
  1. ๋ณ‘์›์ด ์žˆ๋Š” ์ง€์—ญ๋ณ„๋กœ ์ ‘์ข…์˜ˆ์•ฝ๊ฑด์ˆ˜์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•จ.
  2. ๋ชจ๋“  ๋ณ‘์›์˜ ์ ‘์ข…์˜ˆ์•ฝ๊ฑด์ˆ˜๋ฅผ ํ•ฉ์ณ์„œ ์ดํ•ฉ์„ ์ถœ๋ ฅํ•จ.
  3. (๋ณ€ํ˜•) ๊ธฐ์กด ๋ฌธ์ œ๋Š” ๊ด‘์ฃผ์ง€์—ญ์˜ ๊ฒฝ์šฐ ์˜ˆ์•ฝ๊ฑด์ˆ˜๊ฐ€ ์—†์–ด ์ถœ๋ ฅ์„ ํ•˜์ง€ ์•Š์•˜์œผ๋‚˜, ์˜ˆ์•ฝ์ด ์—†๋Š” ๊ฒฝ์šฐ 0์œผ๋กœ ํ‘œ๊ธฐํ•˜์—ฌ ์ถœ๋ ฅํ•จ.

SELECT 

	H.hospaddr AS ๋ณ‘์›์ง€์—ญ, 
    DECODE(H.hospaddr,'10','์„œ์šธ','20','๋Œ€์ „','30','๋Œ€๊ตฌ','40','๊ด‘์ฃผ') AS ๋ณ‘์›์ง€์—ญ๋ช…, 
    NVL(count(V.hospcode),'0') AS ์ ‘์ข…์˜ˆ์•ฝ๊ฑด์ˆ˜ 
    
FROM TBL_HOSP_202108 H 
	LEFT JOIN TBL_VACCRESV_202108 V 
    ON H.hospcode = V.hospcode 
    
GROUP BY ROLLUP(hospaddr);

์ด๋ ‡๊ฒŒ ROLLUP ์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์•˜๋Š”๋ฐ์š” ์•„์ฃผ ์œ ์šฉํ•˜๊ณ  ์ž์ฃผ ์“ฐ์ด๊ฒŒ๋  ํ•จ์ˆ˜์ค‘์— ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

 

์ž์„ธํ•œ ์„ค๋ช…๊ณผ ์˜ˆ์ œ๋Š” ์•„๋ž˜์˜ ์ดˆ๊ณ ์ˆ˜๋‹˜์˜ ๋ธ”๋กœ๊ทธ์—์„œ ํ™•์ธํ•˜์‹ค์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.

 

์ •๋ฆฌ๋ฅผ ์ž˜ํ•ด์ฃผ์…”์„œ ์ดํ•ดํ•˜๋Š”๋ฐ ๋งŽ์€ ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.๐Ÿ˜Š

 

https://gent.tistory.com/57

 

[Oracle] ์˜ค๋ผํด ROLLUP ํ•ฉ๊ณ„, ์†Œ๊ณ„ ๊ตฌํ•˜๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

์˜ค๋ผํด GROUP BY ์ฟผ๋ฆฌ์—์„œ ROLLUP ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์†์‰ฝ๊ฒŒ ํ•ฉ๊ณ„์™€ ์†Œ๊ณ„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. 1. GROUP BY ์ปฌ๋Ÿผ์ด ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ - GROUP BY ์ ˆ์— ROLLUP ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ ํ›„ ๊ทธ๋ฃนํ•‘ ์ปด๋Ÿผ์„ ์ธ์ž๋กœ ๋„ฃ๋Š”๋‹ค SELE.

gent.tistory.com

 

300x250

์ฝ”๋“œ