DAY-26. Oracle ์ธ๋ผ์ธ ๋ทฐ, rownum
๐ 2022-04-06 -1
๋ฐ์ดํฐ๋ฒ ์ด์ค
โ ์์ ์ ๋ฃ๊ณ , ๊ฐ์ธ์ด ๊ณต๋ถํ ๋ด์ฉ์ ์ ์ ๊ฒ ์ด๊ธฐ์ ์ค๋ฅ๊ฐ ๋ง์ ์๋ ์์
๐ ์ธ๋ผ์ธ ๋ทฐ (inline - view)
์๋ธ์ฟผ๋ฆฌ๊ฐย FROM ์ ์์ ์์์ ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ,ย ํด๋น ์๋ธ์ฟผ๋ฆฌ๋ฅผ โ์ธ๋ผ์ธ๋ทฐโ ๊ธฐ์กด์ ์๋ ์๋ธ์ฟผ๋ฆฌ๋ where ์ ์
- ์ธ๋ผ์ธ๋ทฐ๋ฅผ ์ฌ์ฉํ ๋๋ ์ธ๋ผ์ธ๋ทฐ์์ ์กฐํํด์จ ๋ฐ์ดํฐ์ ๋ํด์๋ง ๋ฉ์ธ์ฟผ๋ฆฌ๋ฌธ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์
- from ์ ์์ ๋จผ์ ์ค์ํ๊ณ ๊ฐ์ํ ์ด๋ธ(emp_name, salary๋ง ์๋)์ ๋ง๋ ๊ฒ์ select ํ๋ค๊ณ ์๊ฐ
select
emp_no // ์ค๋ฅ ๋ฐ์
from (select emp_name, salary from employee);
select
* // emp_name๊ณผ salary๋ง ๋ณด์ฌ์ง
from (select emp_name, salary from employee);
๐ก ์คํ์์ : from โ where โ group by โ having โ select โ order by
๐ Top N ๋ถ์ (Top N Query)
- ์ปฌ๋ผ์์ ๊ฐ์ฅ ํฐ n๊ฐ์ ๊ฐ์ ํน์ ๊ฐ์ฅ ์์ n๊ฐ์ ๊ฐ์ ์์ฒญํ ๋ ์ฌ์ฉํ๋ ์ฟผ๋ฆฌ
- ์์/ํ์ n๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๋ ์ฟผ๋ฆฌ
- ํ์ฌ์์ ๊ฐ์ฅ ๋ง์ด ํ๋ฆฐ ์ ํ 10๊ฐ๋ ๋ฌด์์ธ๊ฐ? ํ์ฌ์์ ์๊ธ์ ์ ์ผ ๋ง์ด ๋ฐ๋ ์ฌ๋ 3๋ช ์ ๋๊ตฌ์ธ๊ฐ?
- ๊ฒ์ํ ํ์ด์ง / ์กฐํ์๊ฐ ๋์ ์ธ๊ธฐ๊ธ
// emplyoee ํ
์ด๋ธ์์ ๊ฐ์ฅ ๋์ ๊ธ์ฌ๋ฅผ ๋ฐ๋ ์ฌ๋ 5๋ช
select
emp_name, salary
from employee
order by 2 desc;
1๏ธโฃ rownum
- ์ถ๋ ฅ๋๋ select ๋ฌธ์ ํ๋ง๋ค ์๋์ ์ผ๋ก ์์๋ฅผ ๋งค๊ฒจ์ฃผ๋๊ฒ
- default๋ก๋ ์๋ ์กด์ฌํ๋ ๋ฐ์ดํฐ์ ์์๋๋ก ์์๋ฅผ ๋งค๊ฒจ์ค
โ ์ค๋ฅ
// ์๊ธ์ด ํฐ ์ฌ๋๋ถํฐ ์์๋ฅผ ๋งค๊ฒจ๋ณด์ -> order by
select
rownum,
emp_name, salary
from employee
order by 3 desc;
- โ rownum์ order by์ ์ฉ ์ด์ ์ ๋งค๊ฒจ์ง๋ค. ์์๊ฐ ๋จผ์ ๋งค๊ฒจ์ง๊ณ ๋์ ORDER BY๋ก ์ ๋ ฌ๋๋ฏ๋ก, rownum๊ณผ order by์ ๋์์ฌ์ฉ์ ๊ฒฐ๊ณผ ์์๋ฅผ ๋ค์ฃฝ๋ฐ์ฃฝ์ธ ๊ฒ์ฒ๋ผ ๋ง๋ ๋ค
- select ์ ์ด order by ์ ๋ณด๋ค ๋จผ์ ์คํ๋๋ฉด์ ์ด๋ฏธ rownum์ด ๋งค๊ฒจ์ง ํ์ ๋ค์ ์ฌ์ ๋ ฌ๋๊ธฐ ๋๋ฌธ์ ์์๊ฐ ๋ค์ฃฝ๋ฐ์ฃฝ
๐ก ํด๊ฒฐ
// ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ธฐ์ค์ ๋ง์ถฐ ์ด๋ฏธ ์ ๋ ฌ์ด ๋ ์ํ์ ๋ฐ์ดํฐ์ ๋ํด rownum -> ์์๋ฅผ ๋งค๊ฒจ์ผ ํจ
// ์ธ๋ผ์ธ๋ทฐ -> ์๋ธ์ฟผ๋ฆฌ ์์์ ์๊ธ์ด ์ ์ผ ํฐ ์์๋๋ก ์ผ๋จ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฌ
// -> ์ค์ ๋ฉ์ธ์ฟผ๋ฆฌ rownum ์ฌ์ฉํ๊ฒ ๋๋ฉด ์ด๋ฏธ ์ ๋ ฌ๋ ๋ฐ์ดํฐ์ ๋ํด ์์๊ฐ ๋งค๊ฒจ์ง
select * from employee order by salary desc;
select
rownum , emp_name, salary
from (select * from employee order by salary desc);
// rownum์ ์๋ ๊ฐ์ง๊ณ ์๋ ํ
์ด๋ธ์ 1,2,3,4,5 ์์ผ๋ก ์๋ฒ์ ์ ํด์ค๋ค
// ๋จผ์ salary๋ก ์์๋ฅผ ์ ํด์ฃผ๊ณ ๊ทธ ํ
์ด๋ธ์ rownum์ ํด์ค๋ค
// ์์ 5๋ช
์ฐ๋ด ์์
select rownum,
emp_name,
salary
from (select * from employee order by salary desc)
where rownum <= 5;
- select๋ณด๋ค from ์ ์ด ๋จผ์ ์คํ ๋๋ค.
- from์ ๋ก๋ถํฐ ๋จผ์ ๊ฐ์์ ํ ์ด๋ธ์ด ์์ฑ๋ํ rownum๊ณผ emp_name, salary๋ฅผ ๋ฝ๋๋ค
- from ์คํ โ where ์คํ โ select ์คํ
2๏ธโฃ row_number() over()
- row_number() over (order by ์ปฌ๋ผ..); over() ์์ชฝ์ ์ปฌ๋ผ ์์์ ๋ฐ๋ผ ์ผ๋จ ์ ๋ ฌ์ ํ ํ์ ์์๋ฅผ ๋งค๊ฒจ์ค
- row_number()๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ์ ๊ฐ(์ค๋ณต ๋ฐ์ดํฐ)์ด์ฌ๋ ์์๊ฐ ๋๋์ด์ง๋ค โ ๊ฐ์ 200์ด์ฌ๋ ํ๋๋ 19์ ํ๋๋ 20์
select
row_number() over(order by salary desc)"์์", emp_name, salary
from employee;
โ ์ค๋ฅ
๐
// ---- ์๊ธ์ ๊ฐ์ฅ ๋ง์ด ๋ฐ๋ top5
select
row_number() over(order by salary desc) "์์",
emp_name,
salary
from employee
where rownum <= 5 // ์ด๋ ๊ฒ ํ๋ฉด employee ํ
์ด๋ธ์์์ ๊ทธ๋ฅ 5๋ช
์ ์๋ฅธ๊ฑฐ์
- where ์ ์์์ rownum <=5 ๋ฅผ ํด๋ฒ๋ฆฌ๋ฉด employee ํ ์ด๋ธ์์์ ๊ทธ๋ฅ 5๋ช ์ ์๋ฅด๊ณ โ ๊ทธ ๋ค์ ๊ทธ ์์์ salary ์์๋ฅผ ์ ํ๊ฑฐ์ โ ์๋ชป๋ ์ฟผ๋ฆฌ๋ฌธ
- 5๋ช ์์์ ์์๋ฅผ ๋๋๋๊ฒ ์๋ โ ์ ์ฒด ์ธ์์ค ์๊ธ top5๋ฅผ ์ฐพ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์
๐ก ํด๊ฒฐ
๐ // 1๋ฒ
select
์์, emp_name, salary // ์ฌ๊ธฐ์ ์์(rownum)๋ row_number์์ ๊ฐ์ ธ์จ๊ฒ, emp_name,salary๋ employee *์์ ์จ ๋ฐ์ดํฐ
from (select row_number() over(order by salary desc)"์์" , employee.* from employee)
where "์์" <= 5;
// employee.* : ๋ค๋ฅธ ๋ฐ์ดํฐ๋ ์ปฌ๋ผ๊ณผ ํจ๊ป *์ ์จ์ฃผ๊ณ ์ถ์ผ๋ฉด ํ
์ด๋ธ๋ช
. *์ ํ์
// salary๋ฅผ desc๋ก ํ๊ณ rownum์ ๋งค๊ฒจ์ฃผ๊ณ , employee๋ก ๋ถํฐ ๋ชจ๋ ๊ฑธ ์ป์ด ์จ ํ
์ด๋ธ๋ก๋ถํฐ
// ์์, emp_name, salary๋ฅผ ์ถ๋ ฅ ํ๋๊ฒ
๐ // 2๋ฒ
select
*
from (select row_number() over(order by salary desc)"์์" ,
emp_name "์ฌ์๋ช
", salary "์๊ธ" from employee);
// 1๋ฒ๊ณผ 2๋ฒ์ ๊ฐ์ ์ฝ๋
๐
// -- 5์ ๋ถํฐ 10์ ์ถ๋ ฅ
select
*
from (select row_number() over(order by salary desc)"์์" ,
emp_name "์ฌ์๋ช
", salary "์๊ธ" from employee)
where "์์" between 5 and 10;
๐
// ์ฐ๋ด (๋ณด๋์ค ํฌํจ -> (salary + (salary * bonus)) * 12 )์ด ๊ฐ์ฅ ๋์ ์์๋๋ก ์์ ๋งค๊น
//10 ~ 15์ ์ฌ์ด์ธ ์ง์๋ค์ ์์, ์ฌ์๋ช
, ์ฐ๋ด, ์ง๊ธ์ฝ๋, ๋ถ์์ฝ๋
select
*
from (select row_number() over(order by (salary + (salary * nvl(bonus,0))) * 12 desc) "์์",
emp_name "์ฌ์๋ช
",
(salary + (salary * nvl(bonus,0))) * 12 "์ฐ๋ด",
job_code "์ง๊ธ์ฝ๋",
nvl(dept_code,'์ธํด') "๋ถ์์ฝ๋" from employee)
where "์์" between 10 and 15;
3๏ธโฃ rank() over()
- ์์๋ฅผ ๋งค๊ธธ ๋ ์ค๋ณต๋ฐ์ดํฐ๊ฐ ์๋ค๋ฉด ๊ฐ์ ์์๋ฅผ ๋งค๊ธฐ๊ณ , ๊ฐ์ ์์๋ฅผ ๋งค๊ธด ๋ฐ์ดํฐ๋งํผ ๊ฑด๋๋ฐ์ด ๋ค์ ์์๋ฅผ ๋งค๊น
4๏ธโฃ dense_rank() over()
- ์ค๋ณต ๋ฐ์ดํฐ์ ๋ํด ๊ฐ์ ์์๋ฅผ ๋งค๊ธฐ๊ณ ๊ทธ ๋ค์ ์์๋ถํฐ๋ ๊ฑด๋๋ ์์ด ์์ฐจ์ ์ผ๋ก ์์๋ฅผ ๋งค๊น โ ์ด ๋ฐ์ดํฐ์ ๊ฐ์์ ๋์์ ๋ฒํธ๊ฐ ์ผ์นํ์ง ์์ ์ ์์
๐ // 1๋ฒ
select
์์, emp_name, salary // ์ฌ๊ธฐ์ ์์(rownum)๋ row_number์์ ๊ฐ์ ธ์จ๊ฒ, emp_name,salary๋ employee *์์ ์จ ๋ฐ์ดํฐ
from (select row_number() over(order by salary desc)"์์" , employee.* from employee)
where "์์" <= 5;
// employee.* : ๋ค๋ฅธ ๋ฐ์ดํฐ๋ ์ปฌ๋ผ๊ณผ ํจ๊ป *์ ์จ์ฃผ๊ณ ์ถ์ผ๋ฉด ํ
์ด๋ธ๋ช
. *์ ํ์
// salary๋ฅผ desc๋ก ํ๊ณ rownum์ ๋งค๊ฒจ์ฃผ๊ณ , employee๋ก ๋ถํฐ ๋ชจ๋ ๊ฑธ ์ป์ด ์จ ํ
์ด๋ธ๋ก๋ถํฐ
// ์์, emp_name, salary๋ฅผ ์ถ๋ ฅ ํ๋๊ฒ
๐ // 2๋ฒ
select
*
from (select row_number() over(order by salary desc)"์์" ,
emp_name "์ฌ์๋ช
", salary "์๊ธ" from employee);
// 1๋ฒ๊ณผ 2๋ฒ์ ๊ฐ์ ์ฝ๋
๐ ์ค์ต
๋ฌธ์ )
- ๊ฐ์ฅ ๋ณด๋์ค๋ฅผ ๋ง์ด ๋ฐ๋ ์์ผ๋ก ์์๋ฅผ ๋งค๊น
- 4~8์ ์ฌ์๋ค์ ์์,์ด๋ฆ, ๊ธ์ฌ, ๋ณด๋์ค๋ฅผ ์ถ๋ ฅ
1) row_number
select
*
from(select
row_number() over(order by nvl(bonus,0) desc)"์์",
emp_name "์ด๋ฆ",
salary "๊ธ์ฌ",
nvl(bonus,0)"๋ณด๋์ค" from employee)
where "์์" between 4 and 8;
- order by์์ null์ด ๊ฐ์ฅ ํฐ ๊ฐ์ผ๋ก ์ธ์
2) rank() over()
select
*
from(select
rank() over(order by nvl(bonus,0) desc)"์์",
emp_name "์ด๋ฆ",
salary "๊ธ์ฌ",
nvl(bonus,0)"๋ณด๋์ค" from employee)
where "์์" between 4 and 8;
3) dense_rank() over()
select
*
from(select
dense_rank() over(order by nvl(bonus,0) desc)"์์",
emp_name "์ด๋ฆ",
salary "๊ธ์ฌ",
nvl(bonus,0)"๋ณด๋์ค" from employee)
where "์์" between 4 and 8;
Leave a comment