๐ ์ค๋์ ํ์ต
1. SQL์์ ๋ณต์กํ ๋ฐ์ดํฐ์ ์ ๋ค๋ฃฐ ๊ฒฝ์ฐ
๋ณต์กํ ๋ฐ์ดํฐ์ ์ ๋ฌธ์ ์
๊ฐ๋ ์ฑ↓ ์ฌ์ฌ์ฉ์ฑ↓ ์ ์ง๋ณด์์ฑ↓ ์ค์↑
2. ์์ํ ์ด๋ธ temporary table
๐ ์์ ํ
์ด๋ธ tmp_order_details ์์ฑ ๐
โ
์ฃผ๋ฌธ๋ฒํธ, ๊ณ ๊ฐ๋ฒํธ, ์ฃผ๋ฌธ์ผ, ์ฐ, ์, ์ผ, ์ ํ๋ฒํธ, ์ ํ๋จ๊ฐ, ์๋, ํ ์ธ์จ, ๋งค์ถ์ก
create temporary table tmp_order_details as
select o.order_id , customer_id , order_date
, to_char(order_date, 'YYYY') as year
, to_char(order_date, 'mm') as month
, to_char(order_date, 'dd') as day
, to_char(order_date, 'q') as quarter
, od.product_id , od.unit_price , od.quantity , od.discount
, od.unit_price * od.quantity * (1-od.discount) as amount
from orders o , order_details od
where o.order_id = od.order_id ;
๐ ์ธ์
์ด ์ ์ง๋๋ ๋์ ์ผ๋ฐ ํ
์ด๋ธ์ฒ๋ผ ์ฌ์ฉ(alter, drop ๊ฐ๋ฅ)
๐ ์์ ํ
์ด๋ธ tmp_order_details ์ด์ฉ ๐
โ
์๋ณ ๋งค์ถ์ก, ์ฃผ๋ฌธ๊ฑด์ ์ถ์ถ, ์ฃผ๋ฌธ์ ์, ๊ฑด๋น ํ๊ท ์ฃผ๋ฌธ์ก, ๊ณ ๊ฐ๋น ํ๊ท ์ฃผ๋ฌธ์ก
select year , month , sum(amount) as ๋งค์ถ์ก, count(distinct order_id) as ์ฃผ๋ฌธ๊ฑด์
, count(distinct customer_id) as ์ฃผ๋ฌธ์์
, sum(amount)/count(distinct order_id) as ๊ฑด๋นํ๊ท ์ฃผ๋ฌธ์ก
, sum(amount)/count(distinct customer_id) as ๊ณ ๊ฐ๋นํ๊ท ์ฃผ๋ฌธ์ก
from tmp_order_details
group by 1, 2;
3. ๊ณตํต ํ ์ด๋ธ ํํ์ CTE(Common Table Expression)
๐ ๊ณตํต ํ
์ด๋ธ cte_order_details ์์ฑ ๐
โ
์ฃผ๋ฌธ๋ฒํธ, ๊ณ ๊ฐ๋ฒํธ, ์ฃผ๋ฌธ์ผ, ์ฐ, ์, ์ผ, ๋ถ๊ธฐ, ์ ํ๋ฒํธ, ์ ํ๋จ๊ฐ, ์๋, ํ ์ธ์จ, ๋งค์ถ์ก
with
cte_order_details as(
select o.order_id , customer_id , order_date
, to_char(order_date, 'YYYY') as year
, to_char(order_date, 'mm') as month
, to_char(order_date, 'dd') as day
, to_char(order_date, 'q') as quarter
, od.product_id , od.unit_price , od.quantity , od.discount
, od.unit_price * od.quantity * (1-od.discount) as amount
from orders o , order_details od
where o.order_id = od.order_id
)
select * from cte_order_details;
๐ ๊ณตํต ํ
์ด๋ธ cte_order_details์ ์ด์ฉํ์ฌ cte_order_info ํ
์ด๋ธ ์์ฑ ๐
with cte_order_details as (
select o.order_id , o.customer_id , o.order_date
, to_char(o.order_date,'YYYY') as year
, to_char(o.order_date,'q') as Quarter
, to_char(o.order_date,'MM') as month
, to_char(o.order_date,'dd') as day
, od.product_id , od.unit_price , od.quantity , od.discount
, od.unit_price*od.quantity*(1-od.discount) as total
from orders o , order_details od
where o.order_id = od.order_id
)
-- ์๋ณ ๋งค์ถ์ก, ์ฃผ๋ฌธ๊ฑด์, ์ฃผ๋ฌธ์์, ๊ฑด๋นํ๊ท ์ฃผ๋ฌธ์ก, ๊ณ ๊ฐ๋นํ๊ท ์ฃผ๋ฌธ์ก
, cte_order_info as (
select year, month
, sum(total) as ๋งค์ถ์ก
, count(distinct order_id) as ์ฃผ๋ฌธ๊ฑด์
, count(distinct customer_id) as ์ฃผ๋ฌธ์์
, sum(total)/count(distinct order_id) as ๊ฑด๋นํ๊ท ์ฃผ๋ฌธ์ก
, sum(total)/count(distinct customer_id) as ๊ณ ๊ฐ๋นํ๊ท ์ฃผ๋ฌธ์ก
from cte_order_details
group by year, month
)
select sum(๋งค์ถ์ก) as ๋งค์ถ์ก
, sum(์ฃผ๋ฌธ๊ฑด์) as ์ฃผ๋ฌธ๊ฑด์
from cte_order_info;
4. ์๋์ฐ ํจ์
'STARTERS 4๊ธฐ > [STARTERS] TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[STARTERS 4๊ธฐ TIL] #49์ผ์ฐจ(23.04.14) (0) | 2023.04.14 |
---|---|
[STARTERS 4๊ธฐ TIL] #48์ผ์ฐจ(23.04.13) (0) | 2023.04.13 |
[STARTERS 4๊ธฐ TIL] #46์ผ์ฐจ(23.04.11) (1) | 2023.04.11 |
[STARTERS 4๊ธฐ TIL] #45์ผ์ฐจ(23.04.10) (1) | 2023.04.11 |
[STARTERS 4๊ธฐ TIL] #44์ผ์ฐจ(23.04.07) (0) | 2023.04.10 |