[STARTERS 4๊ธฐ TIL] #50์ผ์ฐจ(23.04.17)


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

๊ณ ๊ฐ ๋ถ„์„

1๏ธโƒฃ ๊ตญ๊ฐ€ ๋ณ„ ๊ณ ๊ฐ ์ˆ˜, ๋ˆ„์  ํ•ฉ๊ณ„

2๏ธโƒฃ ๊ตญ๊ฐ€ ๋ณ„ ๊ณ ๊ฐ ์ˆ˜ , ๊ตฌ์„ฑ๋น„, ๋ˆ„์ ๋น„

3๏ธโƒฃ ๊ตฌ๋งค ์ด๋ ฅ์ด ์—†๋Š” ๊ณ ๊ฐ

๐Ÿ“Œ 1๏ธโƒฃ ๊ตญ๊ฐ€ ๋ณ„ ๊ณ ๊ฐ ์ˆ˜, ๋ˆ„์  ํ•ฉ๊ณ„ ๐Ÿ“Œ

select *, sum("๊ณ ๊ฐ ์ˆ˜") over (order by "๊ณ ๊ฐ ์ˆ˜" desc, country)
from (
	select c.country , count(c.customer_id) as "๊ณ ๊ฐ ์ˆ˜"
	from customers c 
	group by c.country 
	order by "๊ณ ๊ฐ ์ˆ˜" desc
	) a;
๐Ÿ“Œ 2๏ธโƒฃ ๊ตญ๊ฐ€ ๋ณ„ ๊ณ ๊ฐ ์ˆ˜, ๊ตฌ์„ฑ๋น„, ๋ˆ„์ ๋น„ ๐Ÿ“Œ

-- ๋ˆ„์  ๊ตฌ์„ฑ๋น„
select *
	 , sum("๊ตฌ์„ฑ๋น„") over (order by "๊ณ ๊ฐ ์ˆ˜" desc, country) as "๋ˆ„์  ๊ตฌ์„ฑ๋น„"
from (
	-- ๊ตฌ์„ฑ๋น„
	select *
	 , "๊ณ ๊ฐ ์ˆ˜" / sum("๊ณ ๊ฐ ์ˆ˜") over() * 100 as "๊ตฌ์„ฑ๋น„"
	from (
		-- ๊ณ ๊ฐ ์ˆ˜
		select c.country , count(c.customer_id) as "๊ณ ๊ฐ ์ˆ˜"
		from customers c 
		group by c.country
		order by "๊ณ ๊ฐ ์ˆ˜" desc
		) a 
	) b;

๐Ÿ“Œ 3๏ธโƒฃ ๊ตฌ๋งค ์ด๋ ฅ์ด ์—†๋Š” ๊ณ ๊ฐ ๐Ÿ“Œ

-- left join ์ด์šฉ
select c.customer_id , c.company_name , o.*
from customers c left join orders o on c.customer_id = o.customer_id 
where o.order_id is null ;

-- ์ฐจ์ง‘ํ•ฉ ์ด์šฉ
(select c.customer_id , c.company_name
from customers c)
except
(select distinct c.customer_id , c.company_name
from customers c join orders o on c.customer_id = o.customer_id) ;

 

 

 

๊ณ ๊ฐ ๊ตฌ๋งค์ง€ํ‘œ ๋ถ„์„

1๏ธโƒฃ ๊ตญ๊ฐ€ ๋ณ„ ๋ถ„์„

2๏ธโƒฃ ์ง€์—ญ ๋ณ„ ๋ถ„์„

3๏ธโƒฃ ๊ณ ๊ฐ ๋ณ„ ๋ถ„์„

๐Ÿ“Œ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ ๐Ÿ“Œ

-- customers : coustomer_id, company_name, cantact_title, country, city
-- orders : order_id, order_date, tear, month, day, quarter
-- order_details : product_id, unit_price, quantity, discount
-- products : product_name
-- categories : category_id, category_name

with cte_customers as (
select c.customer_id , c.company_name , c.contact_title , c.country , c.city 
	 , o.order_id , o.order_date 
	 , to_char(o.order_date, 'YYYY') as year
	 , to_char(o.order_date, 'mm') as month
	 , to_char(o.order_date, 'dd') as day
	 , to_char(o.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
	 , p.product_name 
	 , c2.category_id , c2.category_name 
from customers c , orders o , order_details od , products p , categories c2 
where c.customer_id = o.customer_id 
  and o.order_id = od.order_id 
  and od.product_id = p.product_id 
  and p.category_id = c2.category_id 
 )
select *
from cte_customers;

๐Ÿ“Œ 1๏ธโƒฃ ๊ตญ๊ฐ€๋ณ„ ๊ณ ๊ฐ์ˆ˜, ๋งค์ถœ์•ก, ์ฃผ๋ฌธ๊ฑด์ˆ˜ ์ƒ๊ด€๊ณ„์ˆ˜ ๐Ÿ“Œ

-- customers : coustomer_id, company_name, cantact_title, country, city
-- orders : order_id, order_date, tear, month, day, quarter
-- order_details : product_id, unit_price, quantity, discount
-- products : product_name
-- categories : category_id, category_name

with cte_customers as (
select c.customer_id , c.company_name , c.contact_title , c.country , c.city 
	 , o.order_id , o.order_date 
	 , to_char(o.order_date, 'YYYY') as year
	 , to_char(o.order_date, 'mm') as month
	 , to_char(o.order_date, 'dd') as day
	 , to_char(o.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
	 , p.product_name 
	 , c2.category_id , c2.category_name 
from customers c , orders o , order_details od , products p , categories c2 
where c.customer_id = o.customer_id 
  and o.order_id = od.order_id 
  and od.product_id = p.product_id 
  and p.category_id = c2.category_id 
 )
โœ… ๊ตญ๊ฐ€ ๋ณ„ ๊ณ ๊ฐ์ˆ˜, ๋งค์ถœ์•ก, ์ฃผ๋ฌธ๊ฑด์ˆ˜ : cte_country_customercnt_amount_ordercnt
 , cte_country_customercnt_amount_ordercnt as (
 select country, count(distinct customer_id ) as "๊ณ ๊ฐ์ˆ˜", sum(amount) as "๋งค์ถœ์•ก", count(distinct order_id) as "์ฃผ๋ฌธ๊ฑด์ˆ˜"
 from cte_customers
 group by country
)
โœ… ์ƒ๊ด€๊ด€๊ณ„ : cte_corr_customer_amount_ordercnt
, cte_corr_customer_amount_ordercnt as (
select corr(๊ณ ๊ฐ์ˆ˜, ๋งค์ถœ์•ก) as "๊ณ ๊ฐ์ˆ˜_๋งค์ถœ์•ก"
	 , corr(๋งค์ถœ์•ก, ์ฃผ๋ฌธ๊ฑด์ˆ˜) as "๋งค์ถœ์•ก_์ฃผ๋ฌธ๊ฑด์ˆ˜"
	 , corr(๊ณ ๊ฐ์ˆ˜, ์ฃผ๋ฌธ๊ฑด์ˆ˜) as "๊ณ ๊ฐ์ˆ˜_์ฃผ๋ฌธ๊ฑด์ˆ˜"
from cte_country_customercnt_amount_ordercnt
)
select *
from cte_corr_customer_amount_ordercnt;

โœ… ๊ตญ๊ฐ€ ๋ณ„ ๊ณ ๊ฐ์ˆ˜, ๋งค์ถœ์•ก, ์ฃผ๋ฌธ๊ฑด์ˆ˜
โœ… ์ƒ๊ด€๊ด€๊ณ„

 

๐Ÿ“Œ 2๏ธโƒฃ.1๏ธโƒฃ ์ง€์—ญ ๋ณ„ ๊ณ ๊ฐ์ˆ˜, ๋งค์ถœ์•ก, ์ฃผ๋ฌธ๊ฑด์ˆ˜ ๊ตฌ์„ฑ๋น„ ๐Ÿ“Œ

-- customers : coustomer_id, company_name, cantact_title, country, city
-- orders : order_id, order_date, tear, month, day, quarter
-- order_details : product_id, unit_price, quantity, discount
-- products : product_name
-- categories : category_id, category_name

with cte_customers as (
select c.customer_id , c.company_name , c.contact_title , c.country , c.city 
	 , o.order_id , o.order_date 
	 , to_char(o.order_date, 'YYYY') as year
	 , to_char(o.order_date, 'mm') as month
	 , to_char(o.order_date, 'dd') as day
	 , to_char(o.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
	 , p.product_name 
	 , c2.category_id , c2.category_name 
from customers c , orders o , order_details od , products p , categories c2 
where c.customer_id = o.customer_id 
  and o.order_id = od.order_id 
  and od.product_id = p.product_id 
  and p.category_id = c2.category_id 
 )
 โœ… ๊ตญ๊ฐ€ ๋ณ„ ๊ณ ๊ฐ์ˆ˜, ๋งค์ถœ์•ก, ์ฃผ๋ฌธ๊ฑด์ˆ˜ : cte_country_customercnt_amount_ordercnt
 , cte_country_customercnt_amount_ordercnt as (
 select country, count(distinct customer_id ) as "๊ณ ๊ฐ์ˆ˜", sum(amount) as "๋งค์ถœ์•ก", count(distinct order_id) as "์ฃผ๋ฌธ๊ฑด์ˆ˜"
 from cte_customers
 group by country
)
 โœ… ์ง€์—ญ ์ปฌ๋Ÿผ ์ถ”๊ฐ€ : cte_country_group
, cte_country_group as (
select *
	 , case 
	 	when lower(country) in ('usa', 'canada', 'mexico') then 'NorthAmerica'
	 	when lower(country) in ('brazil', 'venezuela', 'argentina') then 'SouthAmerica'
	 	else 'Europe'
	 end "์ง€์—ญ"
from cte_country_customercnt_amount_ordercnt	 
)
 โœ… ์ง€์—ญ ๋ณ„ ๊ณ ๊ฐ์ˆ˜, ๋งค์ถœ์•ก, ์ฃผ๋ฌธ๊ฑด์ˆ˜ : cte_country_group_customer_amount_ordercnt
, cte_country_group_customer_amount_ordercnt as (
select ์ง€์—ญ, sum(๊ณ ๊ฐ์ˆ˜) as ๊ณ ๊ฐ์ˆ˜ , sum(๋งค์ถœ์•ก) as ๋งค์ถœ์•ก , sum(์ฃผ๋ฌธ๊ฑด์ˆ˜) as ์ฃผ๋ฌธ๊ฑด์ˆ˜
from cte_country_group
group by ์ง€์—ญ
order by ์ง€์—ญ
)
 โœ… ์ง€์—ญ ๋ณ„ ๊ณ ๊ฐ์ˆ˜, ๋งค์ถœ์•ก, ์ฃผ๋ฌธ๊ฑด์ˆ˜ ๊ตฌ์„ฑ๋น„ : cte_country_group_ratio
, cte_country_group_ratio as (
select ์ง€์—ญ
	 , ๊ณ ๊ฐ์ˆ˜
	 , ๊ณ ๊ฐ์ˆ˜ / sum(๊ณ ๊ฐ์ˆ˜) over() * 100 as ๊ณ ๊ฐ์ˆ˜๊ตฌ์„ฑ๋น„
	 , ๋งค์ถœ์•ก
	 , ๋งค์ถœ์•ก / sum(๋งค์ถœ์•ก) over() * 100 as ๋งค์ถœ์•ก๊ตฌ์„ฑ๋น„
	 , ์ฃผ๋ฌธ๊ฑด์ˆ˜
	 , ์ฃผ๋ฌธ๊ฑด์ˆ˜ / sum(์ฃผ๋ฌธ๊ฑด์ˆ˜) over() * 100 as ์ฃผ๋ฌธ๊ฑด์ˆ˜๊ตฌ์„ฑ๋น„ 
from cte_country_group_customer_amount_ordercnt
) 
select *
from cte_country_group_ratio;

โœ… ์ง€์—ญ ์ปฌ๋Ÿผ ์ถ”๊ฐ€
โœ… ์ง€์—ญ ๋ณ„ ๊ณ ๊ฐ์ˆ˜, ๋งค์ถœ์•ก, ์ฃผ๋ฌธ๊ฑด์ˆ˜ / โœ… ์ง€์—ญ ๋ณ„ ๊ณ ๊ฐ์ˆ˜, ๋งค์ถœ์•ก, ์ฃผ๋ฌธ๊ฑด์ˆ˜ ๊ตฌ์„ฑ๋น„

๐Ÿ“Œ 2๏ธโƒฃ.2๏ธโƒฃ ์ง€์—ญ ๋ณ„ ํŒ๋งค๋œ ์ œํ’ˆ ์ˆœ์œ„ ๐Ÿ“Œ

-- customers : coustomer_id, company_name, cantact_title, country, city
-- orders : order_id, order_date, tear, month, day, quarter
-- order_details : product_id, unit_price, quantity, discount
-- products : product_name
-- categories : category_id, category_name

with cte_customers as (
select c.customer_id , c.company_name , c.contact_title , c.country , c.city 
	 , o.order_id , o.order_date 
	 , to_char(o.order_date, 'YYYY') as year
	 , to_char(o.order_date, 'mm') as month
	 , to_char(o.order_date, 'dd') as day
	 , to_char(o.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
	 , p.product_name 
	 , c2.category_id , c2.category_name 
from customers c , orders o , order_details od , products p , categories c2 
where c.customer_id = o.customer_id 
  and o.order_id = od.order_id 
  and od.product_id = p.product_id 
  and p.category_id = c2.category_id 
 )
 โœ… ๊ตญ๊ฐ€, ์นดํ…Œ๊ณ ๋ฆฌ๋ช…, ์ œํ’ˆID, ์ œํ’ˆ๋ช…, ์ˆ˜๋Ÿ‰, ์ง€์—ญ(ํŒŒ์ƒ์ปฌ๋Ÿผ)
 , cte_customer_product_sale as (
 select country, category_name, product_id, product_name, quantity
 	  , case
 	  	when lower(country) in ('usa', 'canada', 'mexico') then 'NorthAmerica'
	 	when lower(country) in ('brazil', 'venezuela', 'argentina') then 'SouthAmerica'
	 	else 'Europe'
 	  end as "์ง€์—ญ"
 from cte_customers
 )
 โœ… ์ง€์—ญ๋ณ„ ์ œํ’ˆ ํŒ๋งค์ˆ˜๋Ÿ‰
 , cte_country_group_product_quantity as (
 select ์ง€์—ญ, '[ '||category_name||'] '||product_name||'('||product_id::varchar(10)||')' as "์ œํ’ˆ"
 	  , sum(quantity) as ํŒ๋งค์ˆ˜๋Ÿ‰
 from cte_customer_product_sale
 group by 1, 2
 )
 โœ… ์ง€์—ญ๋ณ„ ์ œํ’ˆ ํŒ๋งค์ˆ˜๋Ÿ‰ ์ˆœ์œ„
 , cte_country_group_product_quantity_rank as (
 select * , row_number() over(partition by ์ง€์—ญ order by ํŒ๋งค์ˆ˜๋Ÿ‰ desc) as ์ˆœ์œ„
 from cte_country_group_product_quantity
 ) 
 โœ… ์ง€์—ญ๋ณ„ ํŒ๋งค์ˆœ์œ„ ๋น„๊ต(pivot)
 , cte_country_group_product_quantity_rank_pivot as (
 select ์ˆœ์œ„
 	  , max(case when ์ง€์—ญ = 'NorthAmerica' then ์ œํ’ˆ end) as NorthAmerica
 	  , max(case when ์ง€์—ญ = 'SouthAmerica' then ์ œํ’ˆ end) as SouthAmerica
 	  , max(case when ์ง€์—ญ = 'Europe' then ์ œํ’ˆ end) as Europe
 from cte_country_group_product_quantity_rank
 group by ์ˆœ์œ„
 order by ์ˆœ์œ„
 )
select *
from cte_country_group_product_quantity_rank_pivot;

โœ… ์ง€์—ญ๋ณ„ ํŒ๋งค์ˆœ์œ„ ๋น„๊ต(pivot)

 

๐Ÿ“Œ 3๏ธโƒฃ ๊ณ ๊ฐ๋ณ„ ๋ถ„์„ - ๊ธฐ๋ณธ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ ๐Ÿ“Œ

-- customers : coustomer_id, company_name, cantact_title, country, city
-- orders : order_id, order_date, tear, month, day, quarter
-- order_details : product_id, unit_price, quantity, discount
-- products : product_name
-- categories : category_id, category_name

with cte_customers as (
select c.customer_id , c.company_name , c.contact_title , c.country , c.city 
	 , o.order_id , o.order_date 
	 , to_char(o.order_date, 'YYYY') as year
	 , to_char(o.order_date, 'mm') as month
	 , to_char(o.order_date, 'dd') as day
	 , to_char(o.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
	 , p.product_name 
	 , c2.category_id , c2.category_name 
from customers c , orders o , order_details od , products p , categories c2 
where c.customer_id = o.customer_id 
  and o.order_id = od.order_id 
  and od.product_id = p.product_id 
  and p.category_id = c2.category_id 
 )
 select customer_id
 	  , count(distinct order_id) as ์ฃผ๋ฌธ๊ฑด์ˆ˜
 	  , rank() over(order by count(order_id) desc) as ์ฃผ๋ฌธ๊ฑด์ˆ˜์ˆœ์œ„
 	  , sum(amount) as ๋งค์ถœ์•ก
 	  , rank() over(order by sum(amount) desc) as ๋งค์ถœ์•ก์ˆœ์œ„
 	  , sum(amount) / count(distinct order_id) as ๊ฑด๋‹นํ‰๊ท ์ฃผ๋ฌธ์•ก
 	  , rank() over(order by sum(amount) / count(distinct order_id) desc) as ๊ฑด๋‹นํ‰๊ท ์ฃผ๋ฌธ์•ก์ˆœ์œ„
 from cte_customers
 group by customer_id
 order by ๋งค์ถœ์•ก desc;

 

 

 

Decil ๋ถ„์„

๋ฐ์ดํ„ฐ๋ฅผ 10๋‹จ๊ณ„๋กœ ๋ถ„ํ• ํ•˜์—ฌ ์ค‘์š”๋„๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๋ฐฉ๋ฒ•

๐Ÿ“Œ decil ๋ถ„์„ ๐Ÿ“Œ

-- customers : coustomer_id
-- order_details : amount

with cte_customers as (
select c.customer_id
	 , od.unit_price * od.quantity * (1-od.discount) as amount
from customers c , orders o , order_details od 
where c.customer_id = o.customer_id 
  and o.order_id = od.order_id 
 )
 โœ… ๋งค์ถœ์•ก ๊ธฐ์ค€์œผ๋กœ ์ƒ์œ„๋ถ€ํ„ฐ 10%์”ฉ ๋‚˜๋ˆ„์–ด 10๊ฐœ์˜ ๊ทธ๋ฃน ํ• ๋‹น
 , cte_customer_info as (
 select customer_id
 	  , sum(amount) as ๋งค์ถœ์•ก
 	  , ntile(10) over(order by sum(amount) desc) as decil
 from cte_customers
 group by customer_id
 )
 โœ… decil ๋ณ„ ๋งค์ถœํ•ฉ๊ณ„
 , cte_decil_group as (
 select decil, sum(๋งค์ถœ์•ก) as decil_sum_amount
 from cte_customer_info
 group by decil
 )
  โœ… decil ๋ณ„ ๊ตฌ์„ฑ๋น„
 , cte_decil_ratio as (
 select * 
 	  , sum(decil_sum_amount) over () as total
 	  , decil_sum_amount / sum(decil_sum_amount) over () * 100 as decil_sum_amount_rate
 from cte_decil_group
 )
  โœ… decil ๋ณ„ ๊ตฌ์„ฑ๋น„ ๋ˆ„๊ณ„
, cte_decil_agg as (
select *
	 , sum(decil_sum_amount_rate) over (order by decil) as cumsum
from cte_decil_ratio
order by decil
)
select *
from cte_decil_agg ;

 

Decil ๋ถ„์„์˜ ๋‹จ์ 

โœ”๏ธ ํ•œ ๋ฒˆ์˜ ๊ตฌ๋งค๋กœ ๋น„์‹ผ ๋ฌผ๊ฑด์„ ๊ตฌ๋งคํ•œ ์‚ฌ์šฉ์ž์™€ ์ •๊ธฐ์ ์œผ๋กœ ์ €๋ ดํ•œ ๋ฌผ๊ฑด์„ ์—ฌ๋Ÿฌ๋ฒˆ ๊ตฌ๋งคํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ๊ฐ™์€ ๊ทธ๋ฃน์œผ๋กœ ํŒ์ •๋˜๋Š” ๋ฌธ์ œ

โœ”๏ธ ๊ฒ€์ƒ‰๊ธฐ๊ฐ„์ด ๋„ˆ๋ฌด ์žฅ๊ธฐ๊ฐ„์ด๋ฉด ๊ณผ๊ฑฐ์—๋Š” ์šฐ์ˆ˜๊ณ ๊ฐ์ด์—ˆ์–ด๋„ ํ˜„์žฌ๋Š” ๋‹ค๋ฅธ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํœด๋จผ๊ณ ๊ฐ์ด ํฌํ•จ๋  ์ˆ˜ ์žˆ์Œ

โœ”๏ธ ๊ฒ€์ƒ‰๊ธฐ๊ฐ„์ด ๋„ˆ๋ฌด ๋‹จ๊ธฐ๊ฐ„์ด๋ฉด ์ •๊ธฐ์ ์œผ๋กœ ๊ตฌ๋งคํ•˜๋Š” ์•ˆ์ •๊ณ ๊ฐ๋ณด๋‹ค ํ•ด๋‹น ๊ธฐ๊ฐ„ ๋™์•ˆ ์ผ์‹œ์ ์œผ๋กœ ๋งŽ์ด ๊ตฌ๋งคํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ์šฐ์ˆ˜๊ณ ๊ฐ์œผ๋กœ ํฌํ•จ๋  ์ˆ˜ ์žˆ์Œ

 

 

 

RFM ๋ถ„์„

โœ”๏ธ Recency : ์–ผ๋งˆ๋‚˜ ์ตœ๊ทผ์— ๊ตฌ๋งคํ–ˆ๋Š”๊ฐ€?

โœ”๏ธ Frequency : ์–ผ๋งˆ๋‚˜ ๋นˆ๋ฒˆํ•˜๊ฒŒ ๊ตฌ๋งคํ–ˆ๋Š”๊ฐ€?

โœ”๏ธ Monetary : ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ๊ธˆ์•ก์„ ์ง€๋ถˆํ–ˆ๋Š”๊ฐ€?

· ๊ตฌ๋งค ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์€ ๊ณ ๊ฐ์„ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ๋ถ„์„ ๋ฐฉ๋ฒ•

· ๋งˆ์ผ€ํŒ…์—์„œ ์‚ฌ์šฉ์ž ํ‹ฐ์ผ“ํŒ…์„ ์œ„ํ•œ ๋ฐฉ๋ฒ•

 โžก๏ธ Decil ๋ถ„์„์˜ ๋‹จ์  ๋ณด์™„

 

๐Ÿ“Œ RFM ๋ถ„์„ ๐Ÿ“Œ

-- customers : coustomer_id, company_name, cantact_title, country, city
-- orders : order_id, order_date, tear, month, day, quarter
-- order_details : product_id, unit_price, quantity, discount
-- products : product_name
-- categories : category_id, category_name

with cte_customers as (
select c.customer_id , c.company_name , c.contact_title , c.country , c.city 
	 , o.order_id , o.order_date 
	 , to_char(o.order_date, 'YYYY') as year
	 , to_char(o.order_date, 'mm') as month
	 , to_char(o.order_date, 'dd') as day
	 , to_char(o.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
	 , p.product_name 
	 , c2.category_id , c2.category_name 
from customers c , orders o , order_details od , products p , categories c2 
where c.customer_id = o.customer_id 
  and o.order_id = od.order_id 
  and od.product_id = p.product_id 
  and p.category_id = c2.category_id 
 ) 
โœ… ๊ธฐ์ค€์ผ ์„ ์ •
, cte_customer_maxo as (
select *, max(order_date) over() as maxo
from cte_customers
)
โœ… RFM ์‚ฐ์ถœ
, cte_rfm as (
select customer_id 
	 , max(maxo) - max(order_date) as Recency
	 , count(distinct order_id) as Frequency
	 , sum(amount) as Monetary
from cte_customer_maxo
group by customer_id 
order by 2 , 3 desc, 4 desc
)
select *
from cte_rfm;

 

 

 

 

๐Ÿ“ ์˜ค๋Š˜์˜ ๊ณผ์ œ

left joinํ•œ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„์„
โฏ inner join์œผ๋กœ ๋งŒ๋“  ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”

select c.customer_id , c.company_name , c.contact_title , c.country , c.city 
	 , o.order_id , o.order_date 
	 , to_char(o.order_date, 'YYYY') as year
	 , to_char(o.order_date, 'mm') as month
	 , to_char(o.order_date, 'dd') as day
	 , to_char(o.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
	 , p.product_name 
	 , c2.category_id , c2.category_name 
from customers c , orders o , order_details od , products p , categories c2 
where c.customer_id = o.customer_id 
  and o.order_id = od.order_id 
  and od.product_id = p.product_id 
  and p.category_id = c2.category_id
โฉ left join์œผ๋กœ ๋‚˜ํƒ€๋‚ธ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”

select c.customer_id , c.company_name , c.contact_title , c.country , c.city 
	 , o.order_id , o.order_date 
	 , to_char(o.order_date, 'YYYY') as year
	 , to_char(o.order_date, 'mm') as month
	 , to_char(o.order_date, 'dd') as day
	 , to_char(o.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
	 , p.product_name 
	 , c2.category_id , c2.category_name 
from customers c 
	left join orders o on c.customer_id = o.customer_id 
	left join order_details od on o.order_id = od.order_id 
	left join products p on od.product_id = p.product_id 
	left join categories c2 on p.category_id = c2.category_id ;

๐ŸŒŸ ๋‘ ๊ธฐ๋ณธ ํ…Œ์ด๋ธ”์˜ ์ฐจ์ด์  : left join์€ ์ฃผ๋ฌธ ์ด๋ ฅ์ด ์—†๋Š” ๊ณ ๊ฐ๋„ ๋ชจ๋‘ ํฌํ•จํ•จ

 

 

 

๊ณ ๊ฐ๋ณ„ ABC ๋ถ„์„
๐Ÿ“Œ ๊ณ ๊ฐ๋ณ„ ABC ๋ถ„์„ ๐Ÿ“Œ

-- customers : coustomer_id, company_name, cantact_title, country, city
-- orders : order_id, order_date, tear, month, day, quarter
-- order_details : product_id, unit_price, quantity, discount
-- products : product_name
-- categories : category_id, category_name

with cte_customers as (
select c.customer_id , c.company_name , c.contact_title , c.country , c.city 
	 , o.order_id , o.order_date 
	 , to_char(o.order_date, 'YYYY') as year
	 , to_char(o.order_date, 'mm') as month
	 , to_char(o.order_date, 'dd') as day
	 , to_char(o.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
	 , p.product_name 
	 , c2.category_id , c2.category_name 
from customers c , orders o , order_details od , products p , categories c2 
where c.customer_id = o.customer_id 
  and o.order_id = od.order_id 
  and od.product_id = p.product_id 
  and p.category_id = c2.category_id 
 )
 โœ… ๋งค์ถœ์•ก ์‚ฐ์ถœ
 , cte_customer_sales as (
 select customer_id
 	  , sum(amount) as ๋งค์ถœ์•ก
 from cte_customers
 group by customer_id
 order by ๋งค์ถœ์•ก desc
 )
 โœ… ๊ตฌ์„ฑ๋น„ ์‚ฐ์ถœ
 , cte_customer_ratio as (
 select * 
 	  , ๋งค์ถœ์•ก / sum(๋งค์ถœ์•ก) over () * 100 as ๊ตฌ์„ฑ๋น„
 from cte_customer_sales
 )
  โœ… ๊ตฌ์„ฑ๋น„ ๋ˆ„๊ณ„ ์‚ฐ์ถœ
 , cte_customer_agg as (
 select *
 	  , sum(๊ตฌ์„ฑ๋น„) over (order by ๊ตฌ์„ฑ๋น„ desc) as ๊ตฌ์„ฑ๋น„๋ˆ„๊ณ„
 from cte_customer_ratio
 )
  โœ… ๋“ฑ๊ธ‰ ์‚ฐ์ถœ
 , cte_class as (
 select *
 	  , case
			when ๊ตฌ์„ฑ๋น„๋ˆ„๊ณ„ <= 70 then 'A'
			when ๊ตฌ์„ฑ๋น„๋ˆ„๊ณ„ <= 90 then 'B'
			else 'C'
		end as ๋“ฑ๊ธ‰
from cte_customer_agg
order by ๊ตฌ์„ฑ๋น„ desc
)
select *
from cte_class;