ex23_pseudo
์์ฌ์ปฌ๋ผ, Pseudo Column
- ์ค์ ์ปฌ๋ผ์ด ์๋๋ฐ ์ปฌ๋ผ์ฒ๋ผ ํ๋ํ๋ ์์
rownum
- ์ค๋ผํด ์ ์ฉ
- row num > ํ๋ฒํธ > ๋ ์ฝ๋์ ์ผ๋ จ๋ฒํธ
- from์ ์ด ์คํ๋ ๋ ๊ฐ ๋ ์ฝ๋์ ์ผ๋ จ๋ฒํธ๋ฅผ ํ ๋นํ๋ค.(*****)
- where์ ์ ์ํฅ์ ๋ฐ์ผ๋ฉด ์ผ๋ จ๋ฒํธ๋ฅผ ๋ค์ ์ ๋นํ๋ค.(reindexing) (*****)
- rownum์ ์ฌ์ฉ > ์๋ธ ์ฟผ๋ฆฌ๋ฅผ ์์ฃผ ์ฌ์ฉํ๋ค.
select
name, buseo, --์ปฌ๋ผ(์์ฑ) > output > ๊ฐ์ฒด์ ํน์ฑ์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ฐ์ ๊ฐ์ง๋ค.(๊ฐ์ธ ๋ฐ์ดํฐ)
sysdate, --ํจ์ > output > ๋ชจ๋ ๋ ์ฝ๋๊ฐ ๋์ผํ ๊ฐ์ ๊ฐ์ง๋ค.(์ ์ ๋ฐ์ดํฐ)
substr(name, 2), --ํจ์ > input + output > ๊ฐ์ฒด๋ง๋ค ๋ค๋ฅธ ๊ฐ์ ๊ฐ์ง๋ค.
'์์', --์์ > output > ๋ชจ๋ ๋ ์ฝ๋๊ฐ ๋์ผํ ๊ฐ์ ๊ฐ์ง๋ค.(์ ์ ๋ฐ์ดํฐ)
rownum --์์ฌ ์ปฌ๋ผ > output > ๊ฐ์ฒด์ ํน์ฑ์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ฐ์ ๊ฐ์ง๋ค.(๊ฐ์ธ ๋ฐ์ดํฐ)
from tblInsa;
-- ๊ฒ์ํ > ํ์ด์ง
-- 1ํ์ด์ง > rownum between 1 and 20
-- 2ํ์ด์ง > rownum between 21 and 40
-- 3ํ์ด์ง > rownum between 41 and 60
select name, buseo, rownum from tblInsa;
select name, buseo, rownum from tblInsa where rownum = 1; --์์น๋ก ๊ฒ์
select name, buseo, rownum from tblInsa where rownum <= 5;
--์๋ ์๋์ด
select name, buseo, rownum from tblInsa where rownum = 10;
select name, buseo, rownum from tblInsa where rownum > 5;
select
name, buseo, rownum
from tblInsa; --1. *** ์ด ์์ ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ rownum์ด ์ด๋ฏธ ํ ๋น๋์ด ์๋ค.
select
name, buseo, rownum --3. ์๋น
from tblInsa --1. ์์ฑ+(rownum)
where rownum = 1; --2. ์กฐ๊ฑด
select
name, buseo, rownum
from tblInsa --1. ์์ฑ(+rownum)
where rownum = 1; -- rownum์ ์กฐ๊ฑด์ผ๋ก ์ฌ์ฉ > ๋ฐ๋์ 1~์กฐ๊ฑด ๋ฒ์ > ์กฐ๊ฑด์ ํฌํจ
--------------
select
name, buseo, basicpay, rownum
from tblInsa;
select
name, buseo, basicpay, rownum --rownum ์์๋๋ก ์๋์ด
from tblInsa
order by basicpay desc;
-- ๊ธ์ฌ๋ฅผ ๊ฐ์ฅ ๋ง์ด ๋ฐ๋ ์ง์ 5๋ช
~
select
name, buseo, basicpay, rownum, rnum --๋ฐ๊นฅ์ชฝ์ rownum์ด ์์ชฝ rownum์ ๋ฎ์ด์ฐ๊ธฐ
from (select
name, buseo, basicpay, rownum as rnum
from tblInsa
order by basicpay desc)
where rownum <= 5;
-- ์ธ๊ตฌ์๊ฐ ๊ฐ์ฅ ๋ง์ ๋๋ผ 1~3๋ฑ
select * from tblCountry;
-- 1. ์ํ๋ ์ ๋ ฌ
select * from tblCountry where population is not null order by population desc;
select rownum from tblCountry where population is not null order by population desc;
-- 2. ์์ ๊ฒฐ๊ณผ์
์ ๊ฐ์ง๊ณ ํ๋ฒ๋ rownum์ ๋ง๋ค๊ธฐ > from ์ ์คํ
select * from (select * from tblCountry where population is not null order by population desc);
select rownum from (select * from tblCountry where population is not null order by population desc);
select * from (select * from tblCountry where population is not null order by population desc)
where rownum <= 3;
-- tblInsa. ๊ธ์ฌ๊ฐ 3๋ฑ
-- 1. ๊ธ์ฌ ์์ผ๋ก ์ ๋ ฌ
select * from tblInsa order by basicpay desc;
select rownum from tblInsa order by basicpay desc;
-- 2. ์ํ๋ ์์๋๋ก ์ ๋ ฌ ๊ฒฐ๊ณผ์
> from์ ์ ์ฉ > rownum ๋ค์ ๋งค๊ธด๋ค.
select a.*, rownum from (select * from tblInsa order by basicpay desc) a;
select a.*, rownum from (select * from tblInsa order by basicpay desc) a where rownum = 1;
select a.*, rownum from (select * from tblInsa order by basicpay desc) a where rownum <= 5;
select a.*, rownum from (select * from tblInsa order by basicpay desc) a where rownum = 3;
-- 3.
--์์ชฝ์ ์๋ rnum์ ์ ์ ๋ฐ์ดํฐ๊ฐ ๋จ
select * from (select a.*, rownum as rnum from (select * from tblInsa order by basicpay desc) a)
where rnum = 3;
-- tblComedian. 5๋ฒ์งธ ๋ฑ๋ฑํ ์ฌ๋?
select * from tblComedian;
-- 1. ์ ๋ ฌ
select * from tblComedian order by weight desc;
-- 2. ์๋ธ์ฟผ๋ฆฌ > rownum ๋ณ์นญ
select a.*, rownum as rnum from (select * from tblComedian order by weight desc) a;
-- 3. ์๋ธ์ฟผ๋ฆฌ > rownum ๊ณ ์ ์ํค๊ธฐ ์ํด์
select * from (select a.*, rownum as rnum from (select * from tblComedian order by weight desc) a); --์์ฑ
-- ์์ชฝ rnum ์ฌ์ฉํด์ where ์กฐ๊ฑด
select * from (select a.*, rownum as rnum from (select * from tblComedian order by weight desc) a)
where rnum = 5;
์ค๋ต๋ ธํธ
-- 3. tblInsa. ์ฌ์ ์ธ์์๊ฐ (๊ฐ์ฅ ๋ง์ ๋ถ์ ๋ฐ ์ธ์์) ๊ฐ์ ธ์ค์์ค.
-- ์ ๋ ฌ
select buseo, count(*) as c from tblInsa where substr(ssn, 8, 1) = '2' group by buseo order by c desc;
-- ์๋ธ์ฟผ๋ฆฌ > rownum ๋ณ์นญ
select a.*, rownum as rnum from (select buseo, count(*) as c from tblInsa where substr(ssn, 8, 1) = '2' group by buseo order by c desc) a;
-- ์๋ธ์ฟผ๋ฆฌ > rownum ๊ณ ์
select buseo, c
from (select a.*, rownum as rnum
from (select buseo, count(*) as c
from tblInsa where substr(ssn, 8, 1) = '2' group by buseo order by c desc) a)
where rnum = 1;
--๋ต์
select * from (select buseo, count(*) as cnt from tblinsa where substr(ssn, 8, 1) = '2'
group by buseo order by count(*) desc) where rownum = 1;
-- 9. tblinsa. ํ๊ท ๊ธ์ฌ 2์์ธ ๋ถ์์ ์ํ ์ง์๋ค์ ๊ฐ์ ธ์ค์์ค. --***
-- ์ด๋ฌด๋ถ
select buseo, round(avg(basicpay)) from tblInsa group by buseo order by avg(basicpay) desc;
select * from tblInsa group by buseo order by avg(basicpay) desc;
select a.*, rownum as rnum from (select buseo from tblInsa group by buseo order by avg(basicpay) desc) a;
select * from tblInsa where buseo = ();
select * from (select a.*, rownum as rnum from (select buseo, avg(basicpay) from tblInsa group by buseo order by avg(basicpay) desc) a) where rnum = 2;
--๋ต
select * from (select a.*, rownum as rnum from (select buseo, avg(basicpay) from tblinsa
group by buseo
order by avg(basicpay) desc) a) where rnum = 2;
select * from tblInsa;
-- 11. tblinsa. ๋จ์ ์ง์ ์ค์์ ๊ธ์ฌ๋ฅผ 3๋ฒ์งธ๋ก ๋ง์ด ๋ฐ๋ ์ง์๊ณผ 9๋ฒ์งธ๋ก ๋ง์ด ๋ฐ๋ ์ง์์ ๊ธ์ฌ ์ฐจ์ก์ ์ผ๋ง์ธ๊ฐ? ***
select * from tblInsa where substr(ssn, 8, 1) = '1' order by basicpay desc;
select a.*, rownum as rnum from (select * from tblInsa where substr(ssn, 8, 1) = '1' order by basicpay desc) a;
select (select basicpay from (select a.*, rownum as rnum from (select * from tblInsa where substr(ssn, 8, 1) = '1' order by basicpay desc) a) where rnum = 3)
- (select basicpay from (select a.*, rownum as rnum from (select * from tblInsa where substr(ssn, 8, 1) = '1' order by basicpay desc) a) where rnum = 9) as "๊ธ์ฌ ์ฐจ์ก" from dual;
--๋ต
select () - () from dual; --dual ์ด์ฉํด์ผ ๋จ
select
(select basicpay from (select basicpay, rownum as rnum from (select basicpay from tblinsa where substr(ssn, 8, 1) = '1' order by basicpay desc)) where rnum = 3)
-
(select basicpay from (select basicpay, rownum as rnum from (select basicpay from tblinsa where substr(ssn, 8, 1) = '1' order by basicpay desc)) where rnum = 9) as "๊ธ์ฌ ์ฐจ์ก"
from dual;
'ํ๋ก๊ทธ๋๋ฐ ๊ณต๋ถ > Oracle' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ex25] with (0) | 2023.03.30 |
---|---|
[ex24] rank (0) | 2023.03.30 |
[ex22] alter (0) | 2023.03.26 |
[ex21] union (0) | 2023.03.26 |
[ex20] view (0) | 2023.03.26 |