240607 Today I Learn
Z-์ฐจํธ๋ก ๋งค์ถ ์ถ์ด ํ์ธํ๊ธฐ
๐ก Z์ฐจํธ๋?
๋งค์ถ๋ถ์์ ์ํด ์ฌ์ฉํ๋ ์ฐจํธ๋ก, ์๋ณ๋งค์ถ, ๋งค์ถ๋๊ณ, ๊ทธ๋ฆฌ๊ณ ์ด๋ํฉ๊ณ๋ฅผ ํ๋ฒ์ ํ์ํ๋ ๊ทธ๋ํ์ด๋ค.
- ์๋ณ๋งค์ถ (ํ๋)
- ๋งค์ถ๋๊ณ (์ฃผํฉ) : ์๋์ ์ผ๋ก ๋จ๊ธฐ์ ์ถ์ด
- ์ด๋ํฉ๊ณ (ํ์) : ํด๋น ์ ๊ธฐ์ค 1๋
์น ๋์ ํฉ๊ณ
- ์ฅ๊ธฐ์ ์ถ์ด๋ฅผ ์ ์ ์์
- ๊ณ์ ์ฑ, ํ๋ก๋ชจ์ ๋ฑ ๋ณ๋์ฑ์ ์ํํด ์ถ์ธ๋ฅผ ๋ณผ ์ ์์
Z-์ฐจํธ ๊ทธ๋ฆฌ๊ธฐ (์ค์ต)
Sales_Transaction ํ ์ด๋ธ์์ ํธ์ฃผ, ์ดํ๋ฆฌ์์ ์๋ณ ๋งค์ถ, ๋งค์ถ ๋๊ณ ๊ตฌํ๊ธฐ
## 1. ํธ์ฃผ, ์ดํ๋ฆฌ์์ ์๋ณ ๋งค์ถ, ๋งค์ถ ๋๊ณ ๊ตฌํ๊ธฐ
SELECT Country
,DATE_FORMAT(STR_TO_DATE(Date,'%m/%d/%Y'),'%Y-%m') AS dt_month
,SUM(Price*Quantity) as monthly_sales
,ROUND(
SUM(SUM(Price*Quantity)) OVER(PARTITION BY Country ORDER BY
DATE_FORMAT(STR_TO_DATE(Date,'%m/%d/%Y'),'%Y-%m')
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW )
, 2) AS country_cumsum
FROM Sales_Transaction st
WHERE Country IN ('Australia','Italy')
GROUP BY Country,dt_month
ORDER BY Country,dt_month;
Sales_Transaction ํ ์ด๋ธ์์ ํธ์ฃผ, ์ดํ๋ฆฌ์์ ์๋ณ ๋งค์ถ, ๋งค์ถ ๋๊ณ, <์ด๋ ํฉ๊ณ> ๊ตฌํ๊ธฐ
## ํธ์ฃผ, ์ดํ๋ฆฌ์์ ์๋ณ ๋งค์ถ, ๋งค์ถ ๋๊ณ, <์ด๋ ํฉ๊ณ> ๊ตฌํ๊ธฐ
SELECT Country
,DATE_FORMAT(STR_TO_DATE(Date,'%m/%d/%Y'),'%Y-%m') AS dt_month
,SUM(Price*Quantity) as monthly_sales
,ROUND(
SUM(SUM(Price*Quantity)) OVER(PARTITION BY Country ORDER BY
DATE_FORMAT(STR_TO_DATE(Date,'%m/%d/%Y'),'%Y-%m')
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW )
, 2) AS country_cumsum
,ROUND(
SUM(SUM(Price*Quantity)) OVER(PARTITION BY Country
ORDER BY DATE_FORMAT(STR_TO_DATE(Date,'%m/%d/%Y'),'%Y-%m')
ROWS BETWEEN 11 PRECEDING AND CURRENT ROW)
, 2) AS moving_cumsum
FROM Sales_Transaction st
WHERE Country IN ('Australia','Italy')
GROUP BY Country,dt_month
ORDER BY Country,dt_month;
๐ Z-์ฐจํธ ๊ทธ๋ฆฌ๊ธฐ
๋ณดํต SQL์ ์ฌ์ฉํด์ ์ฐจํธ๋ฅผ ๊ทธ๋ฆฌ๋ ๊ฒฝ์ฐ์๋, SQL๋ก ๋ฐ์ดํฐ๋ฅผ ์ถ์ถํ๊ณ ์ด๋ฅผ ์์ ์ดํ ๋ค๋ฅธ ์๊ฐํ ํด๋ก ๊ทธ๋ํํํ๋ค๊ณ ํ๋ค. ๊ฐ์ฅ ์ ๊ทผํ๊ธฐ ์ฌ์ด ์์ ์ ํ์ฉํด ์ถ์ถ๋ ๋ฐ์ดํฐ ์ค ์ด๋ ํฉ๊ณ๊ฐ ๋น๊ต์ ๋ช ํํ ๋๋ฌ๋๋ '์ดํ๋ฆฌ์'์ ์ผ์ด์ค๋ฅผ ์ฐจํธ๋ก ๊ทธ๋ ค๋ดค๋๋ฐ, ์์ ๊ฐ์ ๋ชจ์์ด ๋์๋ค.
์ ํํ z ๋ชจ์์ ์ฐจํธ๋ฅผ ๊ตฌํ๋ ค๋ฉด ์ต๊ทผ 2๋ ์น ๋ฐ์ดํฐ๊ฐ ํ์ํ๋ค. ๊ทธ๋ฌ๋ ์ ๊ทธ๋ํ๋ ๋ณด๋ค์ํผ Z-์ฐจํธ๋ผ๊ณ ๋ณด๊ธฐ์๋ ์์ฌ์ด ๋ชจ์์ด๋ค. ๊ทธ ์ด์ ๋ ๋ฐ์ดํฐ์ ์์ด ๋ถ์กฑํ๊ธฐ ๋๋ฌธ์ด๋ค. ์ดํ๋ฆฌ์์ ๊ฒฝ์ฐ 2018๋ 12์๋ถํฐ 2019๋ 12์๊น์ง ์ด 13๊ฐ์์ ๋ฐ์ดํฐ๊ฐ ๋ด๊ฒจ์๋๋ฐ, ๊ทธ๋ฌ๋ค๋ณด๋ ์ด๋ํฉ๊ณ ์ธก๋ฉด์์ ์ฐจ์ด๊ฐ ํฌ์ง ์์๊ฒ์ด๋ค.
Sales_Transaction ํ ์ด๋ธ์์ ์ดํ๋ฆฌ์์ ์ ํ ์ด๋ฆ๋ณ ์๋ณ ๋งค์ถ, ๋งค์ถ ๋๊ณ, ์ด๋ ํฉ๊ณ ๊ตฌํ๊ธฐ
## ์ดํ๋ฆฌ์์ ์ ํ ์ด๋ฆ๋ณ ์๋ณ ๋งค์ถ, ๋งค์ถ ๋๊ณ, ์ด๋ ํฉ๊ณ ๊ตฌํ๊ธฐ
SELECT ProductName
,DATE_FORMAT(STR_TO_DATE(Date,'%m/%d/%Y'),'%Y-%m') AS dt_month
,SUM(Price*Quantity) as monthly_sales
,ROUND(
SUM(SUM(Price*Quantity)) OVER(PARTITION BY ProductName ORDER BY
DATE_FORMAT(STR_TO_DATE(Date,'%m/%d/%Y'),'%Y-%m')
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW )
, 2) AS country_cumsum
,ROUND(
SUM(SUM(Price*Quantity)) OVER(PARTITION BY ProductName
ORDER BY DATE_FORMAT(STR_TO_DATE(Date,'%m/%d/%Y'),'%Y-%m')
ROWS BETWEEN 11 PRECEDING AND CURRENT ROW)
, 2) AS moving_cumsum
FROM Sales_Transaction st
WHERE Country = 'Italy'
GROUP BY ProductName ,dt_month
ORDER BY ProductName ,dt_month;
YOY ๋ถ์ํ๊ธฐ
๐ก YOY(Year-Over-Year Growth Formula)
์ ๋ ๋๋น ์ฆ๊ฐ๋ฅ ์ผ๋ก ์๋์ ๊ฐ์ด ๊ณ์ฐํ ์ ์๋ค.
→ (์ฌํด ๋งค์ถ - ์ง๋ํด ๋งค์ถ) / ์ง๋ํด ๋งค์ถ X 100
→ (Current Year Earnings — Last Year’s Earnings) / Last Year’s Earnings x 100
→ LEAD, LAG ํ์ฉํ๊ธฐ
WITH current_year AS (
SELECT EXTRACT(YEAR FROM transaction_date) yr,
product_id,
SUM(spend) curr_year_spend
FROM user_transactions
GROUP BY EXTRACT(YEAR FROM transaction_date), product_id
),
current_prev_year AS (
SELECT *,
LAG(curr_year_spend) OVER(PARTITION BY product_id ORDER BY product_id, yr) prev_year_spend
FROM current_year
)
SELECT *,
ROUND((curr_year_spend - prev_year_spend)/prev_year_spend*100.0,2) yoy_rate
FROM current_prev_year;
์ฝํ ์ฒดํํ๊ธฐ
WITH product_spend2 AS (
SELECT category,
product,
SUM(spend) AS total_spend
FROM product_spend
WHERE EXTRACT(YEAR FROM transaction_date)=2022
GROUP BY category, product
),
product_rank AS (
SELECT category, product, total_spend,
RANK() OVER(PARTITION BY category ORDER BY total_spend DESC) rank
FROM product_spend2
)
SELECT category, product, total_spend
FROM product_rank
WHERE rank <= 2;
'๐ Today I Learn > ๐ My SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SQL Challenge ์ธ์ #5 ํผ๋ฒํ ์ด๋ธ (0) | 2024.06.05 |
---|---|
SQL Challenge ์ธ์ #4 ๋ฆฌํ ์ , ์ฝํธํธ ๋ถ์ (0) | 2024.05.30 |
SQL Challenge ์ธ์ #3 ํผ๋(Funnel)๋ถ์ (0) | 2024.05.29 |
SQL Challenge ์ธ์ #2 ์ด๋ํ๊ท ๊ณผ ๋น์จ (0) | 2024.05.27 |
SQL Challenge ์ธ์ #1 ์๋์ฐ ํจ์, WITH์ (0) | 2024.05.27 |