๐ ์ค๋์ ํ์ต
์ ํ/์นดํ ๊ณ ๋ฆฌ ๋งค์ถ ์งํ ๋ถ์
1๏ธโฃ ์ ์ฒด ์ ํ ๋งค์ถ ์์ ๋ฐ ๋งค์ถ ๋น์จ
2๏ธโฃ ์นดํ ๊ณ ๋ฆฌ๋ณ ์ ํ ๋ด์ถ ์์ ๋ฐ ๋งค์ถ ๋น์จ
๐ ์์ ํ
์ด๋ธ cte_products_sale ๐
โ
orders : ์ฃผ๋ฌธ๋ฒํธ, ๊ณ ๊ฐ๋ฒํธ, ์ฃผ๋ฌธ์ผ, ์ฐ, ์, ์ผ, ๋ถ๊ธฐ
โ
order_details : ์ ํ๋ฒํธ ํ๋งค๋จ๊ฐ, ์๋, ํ ์ธ์จ, ๋งค์ถ์ก
โ
categories : ์นดํ
๊ณ ๋ฆฌID, ์นดํ
๊ณ ๋ฆฌ๋ช
โ
products : ์ ํID, ์ ํ๋ช
, ๋ง์คํฐ๋จ๊ฐ, ๋จ์ข
์ฌ๋ถ
โ
suppliers : ๊ณต๊ธ์ID, ๊ณต๊ธ์๋ช
, ๊ตญ๊ฐ, ๋์
with cte_products_sale as(
select o.order_id , o.customer_id , o.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 as ํ๋งค๋จ๊ฐ , od.quantity , od.discount , (od.unit_price * od.quantity * (1-od.discount)) as ๋งค์ถ์ก
, c.category_id , c.category_name
, p.product_id , p.product_name , p.unit_price as ๋ง์คํฐ๋จ๊ฐ , p.discontinued
, s.supplier_id , s.company_name , s.country , s.city
from orders o , order_details od , categories c , products p , suppliers s
where o.order_id = od.order_id
and od.product_id = p.product_id
and p.category_id = c.category_id
and p.supplier_id = s.supplier_id
)
select count(*)
from cte_products_sale;
๐ ์ ํ๋ณ ๋งค์ถ์ก ์์ ๐
with cte_products_sale as(
select o.order_id , o.customer_id , o.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 as ํ๋งค๋จ๊ฐ , od.quantity , od.discount , (od.unit_price * od.quantity * (1-od.discount)) as amount
, c.category_id , c.category_name
, p.product_name , p.unit_price as ๋ง์คํฐ๋จ๊ฐ , p.discontinued
, s.supplier_id , s.company_name , s.country , s.city
from orders o , order_details od , categories c , products p , suppliers s
where o.order_id = od.order_id
and od.product_id = p.product_id
and p.category_id = c.category_id
and p.supplier_id = s.supplier_id
)
โ
1. ๋งค์ถ์ก ๊ณ์ฐ
, cte_amount as (
select category_name, product_id, product_name, company_name, country
, sum(amount) as ๋งค์ถ์ก
from cte_products_sale
group by 1,2,3,4,5
order by product_id
)
โ
2. ์์, ์ ์ฒด ๋งค์ถ์ก ๊ณ์ฐ
, cte_rank_totalamount as (
select *
, rank() over(order by ๋งค์ถ์ก desc) as ๋งค์ถ์ก์์
, sum(๋งค์ถ์ก) over() as ์ ์ฒด๋งค์ถ
from cte_amount
)
โ
3. ๋งค์ถ ๋น์จ ๊ณ์ฐ
, cte_radio as (
select *
, ๋งค์ถ์ก / ์ ์ฒด๋งค์ถ * 100 as ๋งค์ถ๋น์จ
from cte_rank_totalamount
)
โ
4. ์ต์ข
ํ
์ด๋ธ
select category_name, product_id, product_name, company_name, country, ๋งค์ถ์ก, ๋งค์ถ์ก์์
,round(๋งค์ถ๋น์จ::numeric ,2)::varchar(10)||'%' as ๋งค์ถ๋น์จ
from cte_radio
order by 1;
ABC ๋ถ์
ํต๊ณ์ ๋ฐฉ๋ฒ์ ์ํด ๊ด๋ฆฌ๋์์ A, B, C ๊ทธ๋ฃน์ผ๋ก ๋๋๊ณ ,
๋จผ์ A๊ทธ๋ฃน์ ์ต์ค์ ๊ด๋ฆฌ๋์์ผ๋ก ์ ์ ํ์ฌ ๊ด๋ฆฌ๋ ธ๋ ฅ์ ์ง์คํจ์ผ๋ก์จ ๊ด๋ฆฌํจ๊ณผ๋ฅผ ๋์ด๋ ค๋ ๋ถ์๋ฐฉ๋ฒ.
๐ ABC ํ
์คํธ ๐
with cte_products_sale as(
select o.order_id , o.customer_id , o.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 as ํ๋งค๋จ๊ฐ , od.quantity , od.discount , (od.unit_price * od.quantity * (1-od.discount)) as amount
, c.category_id , c.category_name
, p.product_name , p.unit_price as ๋ง์คํฐ๋จ๊ฐ , p.discontinued
, s.supplier_id , s.company_name , s.country , s.city
from orders o , order_details od , categories c , products p , suppliers s
where o.order_id = od.order_id
and od.product_id = p.product_id
and p.category_id = c.category_id
and p.supplier_id = s.supplier_id
)
โ
1. ์ ํ๋ณ ๋งค์ถ์ก
, cte_amount as (
select product_id , product_name
, sum(amount) as ๋งค์ถ์ก
from cte_products_sale
group by 1, 2
order by product_id
)
โ
2. ๊ตฌ์ฑ๋น
, cte_ratio as (
select *
, sum(๋งค์ถ์ก) over() as ์ ์ฒด๋งค์ถ์ก
, ๋งค์ถ์ก/sum(๋งค์ถ์ก) over()*100 as ๊ตฌ์ฑ๋น
from cte_amount
)
โ
3.๊ตฌ์ฑ๋น ๋๊ณ
, cte_ratio_agg as (
select *
, sum(๊ตฌ์ฑ๋น) over (order by ๊ตฌ์ฑ๋น desc) as ๊ตฌ์ฑ๋น๋๊ณ
from cte_ratio
order by ๊ตฌ์ฑ๋น desc
)
โ
4. ๋ฑ๊ธ
, cte_class as (
select *
, case
when ๊ตฌ์ฑ๋น๋๊ณ <= 70 then 'A'
when ๊ตฌ์ฑ๋น๋๊ณ <= 90 then 'B'
else 'C'
end as ๋ฑ๊ธ
from cte_ratio_agg
order by ๊ตฌ์ฑ๋น desc
)
โ
์ต์ข
ํ
์ด๋ธ
select * from cte_class;
Pivot
๐ ์นดํ
๊ณ ๋ฆฌ๋ณ ์์๋ฅผ ํผ๋ฒํ๊ธฐ ๐
with cte_products_sale as(
select o.order_id , o.customer_id , o.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 as ํ๋งค๋จ๊ฐ , od.quantity , od.discount , (od.unit_price * od.quantity * (1-od.discount)) as amount
, c.category_id , c.category_name
, p.product_name , p.unit_price as ๋ง์คํฐ๋จ๊ฐ , p.discontinued
, s.supplier_id , s.company_name , s.country , s.city
from orders o , order_details od , categories c , products p , suppliers s
where o.order_id = od.order_id
and od.product_id = p.product_id
and p.category_id = c.category_id
and p.supplier_id = s.supplier_id
)
โ
1. ๋งค์ถ์ก ๊ณ์ฐ
, cte_amount as (
select category_name, product_id, product_name, company_name, country
, sum(amount) as ๋งค์ถ์ก
from cte_products_sale
group by 1,2,3,4,5
order by product_id
)
โ
2. ์์
, cte_rank_totalamount_category as (
select *
, rank() over(partition by category_name order by ๋งค์ถ์ก desc) as ๋งค์ถ์ก์์
from cte_amount
)
โ
3. 3์๊น์ง ๋ฝ๊ธฐ
, cte_rank_top3 as (
select *
from cte_rank_totalamount_category
where ๋งค์ถ์ก์์ between 1 and 3
)
โ
4. ํผ๋ดํ๊ธฐ
select category_name
โ
1์ ์ด
, max(case when ๋งค์ถ์ก์์ = 1 then product_name end) as "1์"
โ
2์ ์ด
, max(case when ๋งค์ถ์ก์์ = 2 then product_name end) as "2์"
โ
3์ ์ด
, max(case when ๋งค์ถ์ก์์ = 3 then product_name end) as "3์"
from cte_rank_top3
group by 1;
๐ ๋งค์ถ์ก ์์ ๋ณ๋ก ์นดํ
๊ณ ๋ฆฌ ํผ๋ฒํ๊ธฐ ๐
with cte_products_sale as(
select o.order_id , o.customer_id , o.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 as ํ๋งค๋จ๊ฐ , od.quantity , od.discount , (od.unit_price * od.quantity * (1-od.discount)) as amount
, c.category_id , c.category_name
, p.product_name , p.unit_price as ๋ง์คํฐ๋จ๊ฐ , p.discontinued
, s.supplier_id , s.company_name , s.country , s.city
from orders o , order_details od , categories c , products p , suppliers s
where o.order_id = od.order_id
and od.product_id = p.product_id
and p.category_id = c.category_id
and p.supplier_id = s.supplier_id
)
โ
1. ๋งค์ถ์ก ๊ณ์ฐ
, cte_amount as (
select category_name, product_id, product_name, company_name, country
, sum(amount) as ๋งค์ถ์ก
from cte_products_sale
group by 1,2,3,4,5
order by product_id
)
โ
2. ์์
, cte_rank_totalamount_category as (
select *
, rank() over(partition by category_name order by ๋งค์ถ์ก desc) as ๋งค์ถ์ก์์
from cte_amount
)
โ
3. ํผ๋ด, null๊ฐ ์ฒ๋ฆฌ
select ๋งค์ถ์ก์์
, coalesce(max(case when category_name = 'Beverages' then product_name end), '') as "Beverages"
, coalesce(max(case when category_name = 'Condiments' then product_name end), '') as "Condiments"
, coalesce(max(case when category_name = 'Confections' then product_name end), '') as "Confections"
, coalesce(max(case when category_name = 'Dairy Products' then product_name end), '') as "Dairy Products"
, coalesce(max(case when category_name = 'Grains/Cereals' then product_name end), '') as "Grains/Cereals"
, coalesce(max(case when category_name = 'Meat/Poultry' then product_name end), '') as "Meat/Poultry"
, coalesce(max(case when category_name = 'Produce' then product_name end), '') as "Produce"
, coalesce(max(case when category_name = 'Seafood' then product_name end), '') as "Seafood"
from cte_rank_totalamount_category
group by 1
order by 1;
๐ ์ค๋์ ๊ณผ์
1๏ธโฃ ์ ์ฒด ํ๋งค ์๋ ํ10 cte_products_quantity
-- products : product_id, product_name,
-- order_details : quantity, unit_price, discount, ๋งค์ถ์ก
-- categories : category_name
with cte_products_quantity as (
select p.product_id , p.product_name
, od.quantity , od.unit_price , od.discount , od.quantity * od.unit_price * (1-od.discount) as amount
, c.category_name
from products p , order_details od , categories c
where p.product_id = od.product_id
and p.category_id = c.category_id
)
-- 1. ์ ํ๋ณ๋ก ๊ทธ๋ฃจํ
, cte_products_quantity_total as (
select product_id, product_name
, sum(quantity) as total_quantity
, sum(amount) as total_sales
, max(category_name) as category_name
from cte_products_quantity
group by product_id , product_name )
-- 2. ์์ ์ถ๊ฐ ๋ฐ 10์๊น์ง ์ถ์ถ
, cte_products_rank as (
select rank() over (order by total_quantity desc), *
from cte_products_quantity_total
limit 10)
select * from cte_products_rank;
2๏ธโฃ ๊ตญ๊ฐ๋ณ ํ๋งค์๋ ํ5 cte_country_sales
-- suppliers : country
-- products : product_id, product_name,
-- order_details : quantity
with cte_country_sales as (
select c.country , p.product_id , p.product_name
, od.quantity , od.unit_price , od.discount , od.quantity * od.unit_price * (1-od.discount) as amount
from products p , order_details od , orders o, customers c
where p.product_id = od.product_id
and od.order_id = o.order_id
and o.customer_id = c.customer_id
order by c.country)
-- 1. ๊ตญ๊ฐ, ์ ํ ๋ณ๋ก ๊ทธ๋ฃจํ
, cte_country_product as (
select country, product_id, product_name
, sum(quantity) as total_quantity
, sum(amount) as total_sales
from cte_country_sales
group by country, product_id, product_name
order by country, product_id
)
-- 2. ๊ตญ๊ฐ๋ณ ํ๋งค์๋ ์์ ์ปฌ๋ผ ์ถ๊ฐ
, cte_country_rank as (
select rank() over (partition by country order by total_quantity desc, total_sales desc) as rank, *
from cte_country_product)
-- 3. ํผ๋ด ์งํ, ๊ตญ๊ฐ ์ด ๋งค์ถ์ก ์ปฌ๋ผ ์ถ๊ฐ
, cte_country_pivot as (
select country
, max(case when rank = 1 then product_name end) as rank1
, max(case when rank = 2 then product_name end) as rank2
, max(case when rank = 3 then product_name end) as rank3
, max(case when rank = 4 then product_name end) as rank4
, max(case when rank = 5 then product_name end) as rank5
, sum(total_sales) as total_total_sales
from cte_country_rank
group by country
)
-- 4. ์ด ๋งค์ถ์ก ์์ผ๋ก ์ ๋ ฌ
, cte_country_table as (
select country, rank1, rank2, rank3, rank4, rank5
from cte_country_pivot
order by total_total_sales desc)
select * from cte_country_table;
3๏ธโฃ 1997๋
๋ถ๊ธฐ๋ณ ํ๋งค์๋ ํ10 cte_quarter_sales
-- products : product_id, product_name,
-- order_details : quantity
-- orders : order_date, ์ฐ๋, ๋ถ
with cte_quarter_sales as (
select p.product_id , p.product_name
, od.quantity , od.unit_price , od.discount , od.quantity * od.unit_price * (1-od.discount) as amount
, to_char(order_date, 'YYYY') as year
, to_char(order_date, 'q') as quarter
from order_details od , products p , orders o
where od.product_id = p.product_id
and od.order_id = o.order_id
)
-- 1. 1997๋
๋ฐ์ดํฐ ์ถ์ถ
, cte_1997_sales as (
select *
from cte_quarter_sales
where year = '1997')
-- 2. ๊ทธ๋ฃจํ
, cte_group as (
select year, quarter, product_name
, sum(quantity) as total_quantity
, sum(amount) as total_amount
from cte_1997_sales
group by year, quarter, product_name
)
-- 3. ๋ถ๊ธฐ๋ณ ํ๋งค์๋ ์์ ์ ์
, cte_1997_rank as (
select rank() over (partition by quarter order by total_quantity desc, total_amount desc) as rank
, *
from cte_group
)
-- 4. ํผ๋ด
, cte_1997_pivot as (
select rank
, max(case when quarter = '1' then product_name end) as "1997-1๋ถ๊ธฐ"
, max(case when quarter = '2' then product_name end) as "1997-2๋ถ๊ธฐ"
, max(case when quarter = '3' then product_name end) as "1997-3๋ถ๊ธฐ"
, max(case when quarter = '4' then product_name end) as "1997-4๋ถ๊ธฐ"
from cte_1997_rank
group by rank
limit 10
)
select * from cte_1997_pivot;
4๏ธโฃ 1997๋
๋ถ๊ธฐ๋ณ ํ๋งค์๋ ์์๋ณํ cte_quarter_sales
-- products : product_id, product_name,
-- order_details : quantity
-- orders : order_date, ์ฐ๋, ๋ถ
with cte_quarter_sales as (
select p.product_id , p.product_name
, od.quantity , od.unit_price , od.discount , od.quantity * od.unit_price * (1-od.discount) as amount
, to_char(order_date, 'YYYY') as year
, to_char(order_date, 'q') as quarter
from order_details od , products p , orders o
where od.product_id = p.product_id
and od.order_id = o.order_id
)
-- 1. ๋ถ๊ธฐ ๋๋ฒ๋ง
, cte_quarter_no as (
select *
, case
when year = '1996' and quarter = '4' then 1
when year = '1997' and quarter = '1' then 2
when year = '1997' and quarter = '2' then 3
when year = '1997' and quarter = '3' then 4
when year = '1997' and quarter = '4' then 5 end as no
from cte_quarter_sales
)
-- 2. ํ์ ๋ถ๊ธฐ๋ง ์ถ์ถ
, cte_need_year as (
select *
from cte_quarter_no
where not no isnull
)
-- 3. ๊ทธ๋ฃจํ
, cte_group as (
select no, year, quarter, product_name
, sum(quantity) as total_quantity
, sum(amount) as total_sales
from cte_need_year
group by no, year, quarter, product_name
order by no, year, quarter, product_name
)
-- 4. ์์
, cte_rank as (
select rank () over (partition by no order by total_quantity desc, total_sales desc), *
from cte_group)
-- 5. ์์๋ณํ ์ฐ์ถ
, cte_lag as (
select *, lag(rank,1) over(partition by product_name order by no)-rank as "์์๋ณํ"
from cte_rank)
-- 6. ํผ๋ฒ ์งํ
, cte_pivot as (
select rank
, max(case when no = 2 then product_name end) as "1997๋
1๋ถ๊ธฐ"
, max(case when no = 2 then ์์๋ณํ end) as "์์๋ณํ"
, max(case when no = 3 then product_name end) as "1997๋
2๋ถ๊ธฐ"
, max(case when no = 3 then ์์๋ณํ end) as "์์๋ณํ"
, max(case when no = 4 then product_name end) as "1997๋
3๋ถ๊ธฐ"
, max(case when no = 4 then ์์๋ณํ end) as "์์๋ณํ"
, max(case when no = 5 then product_name end) as "1997๋
4๋ถ๊ธฐ"
, max(case when no = 5 then ์์๋ณํ end) as "์์๋ณํ"
from cte_lag
group by rank
order by rank
limit 10
)
select * from cte_pivot;
'STARTERS 4๊ธฐ > [STARTERS] TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[STARTERS 4๊ธฐ TIL] #50์ผ์ฐจ(23.04.17) (0) | 2023.04.17 |
---|---|
[STARTERS 4๊ธฐ TIL] #49์ผ์ฐจ(23.04.14) (0) | 2023.04.14 |
[STARTERS 4๊ธฐ TIL] #47์ผ์ฐจ(23.04.12) (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 |