[STARTERS 4๊ธฐ TIL] #47์ผ์ฐจ(23.04.12)


๐Ÿ“š ์˜ค๋Š˜์˜  ํ•™์Šต

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. ์œˆ๋„์šฐ ํ•จ์ˆ˜