๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณต๋ถ€/Oracle

[ex25] with

ex25.with

 

    [WITH <Sub Query>]                          - with ์ ˆ
    SELECT column_list                          - select ์ ˆ
    FROM table_name                             - from ์ ˆ
    [WHERE search_condition]                    - where ์ ˆ
    [GROUP BY group_by_expression]              - group by์ ˆ
    [HAVING search_condition]                   - having ์ ˆ
    [ORDER BY order_expresstion [ASC|DESC]];    - order by์ ˆ

 


with์ ˆ
    - ์ธ๋ผ์ธ๋ทฐ(from์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ)์— ์ด๋ฆ„์„ ๋ถ™์ด๋Š” ๊ธฐ์ˆ 
    - ๋‹จ๋…์œผ๋กœ๋Š” ๋ชป ์”€. (select๋ฌธ ์•ž์—)
    
    with ์ž„์‹œํ…Œ์ด๋ธ”๋ช… as (
    ๊ฒฐ๊ณผ์…‹ select
    )
    select ๋ฌธ;
    
    with์ ˆ ์‹คํ–‰ ๋ฐฉ์‹
    1. Materialize ๋ฐฉ์‹ > ์ž„์‹œ ํ…Œ์ด๋ธ”์ด 2๋ฒˆ ์ด์ƒ ์‚ฌ์šฉ๋˜๋ฉด ๋‚ด๋ถ€์— ์ž„์‹œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ + ๋ฐ˜๋ณต ์žฌ์‚ฌ์šฉ
    2. Inline ๋ฐฉ์‹ > ์ž„์‹œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ์—†์ด ๋งค๋ฒˆ ์ธ๋ผ์ธ ์ฟผ๋ฆฌ๋ฅผ ๋ฐ˜๋ณต ์‹คํ–‰

 

select * from (select name, buseo, jikwi from tblInsa where city = '์„œ์šธ');

--์œ„์•„๋ž˜ ๊ฒฐ๊ณผ ๋˜‘๊ฐ™์Œ
with seoul as (select name, buseo, jikwi from tblInsa where city = '์„œ์šธ')
select * from seoul;

--์—ฌ๋Ÿฌ๊ฐœ
select * 
    from (select name, age, couple from tblMen where weight < 90) a
        inner join (select name, age, couple from tblWomen where weight > 60) b
            on a.couple = b.name;

--with์ ˆ ์‚ฌ์šฉํ•ด์„œ ์‹ฌํ”Œํ•˜๊ฒŒ (with ํ•˜๋‚˜๋กœ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ž„์‹œํ…Œ์ด๋ธ”)
with a as (select name, age, couple from tblMen where weight < 90), 
     b as (select name, age, couple from tblWomen where weight > 60)
select * from a inner join b on a.couple = b.name;



-- ์ˆœ์œ„ํ•จ์ˆ˜ or rownum 
-- with์ ˆ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•จ

-- ๊ธ‰์—ฌ 5์œ„
-- ์›๋ž˜ ๋ฐฉ์‹
select * from (select
    name, buseo, basicpay,
    rank() over(order by basicpay desc) rnum
from tblInsa) where rnum = 5;

-- with์ ˆ ์‚ฌ์šฉ
with insa as (
    select 
        name, buseo, basicpay,
            rank() over(order by basicpay desc) rnum
        from tblInsa
)
select * from insa where rnum = 5;

'ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ณต๋ถ€ > Oracle' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ex27] transaction  (0) 2023.03.30
[ex26] Hierarchical Query  (0) 2023.03.30
[ex24] rank  (0) 2023.03.30
[ex23] pseudo, rownum  (0) 2023.03.26
[ex22] alter  (0) 2023.03.26