๐Ÿ“’ Today I Learn/๐Ÿ˜Š My SQL

240607 Today I LearnZ-์ฐจํŠธ๋กœ ๋งค์ถœ ์ถ”์ด ํ™•์ธํ•˜๊ธฐ๐Ÿ’ก Z์ฐจํŠธ๋ž€?๋งค์ถœ๋ถ„์„์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ฐจํŠธ๋กœ, ์›”๋ณ„๋งค์ถœ, ๋งค์ถœ๋ˆ„๊ณ„, ๊ทธ๋ฆฌ๊ณ  ์ด๋™ํ•ฉ๊ณ„๋ฅผ ํ•œ๋ฒˆ์— ํ‘œ์‹œํ•˜๋Š” ๊ทธ๋ž˜ํ”„์ด๋‹ค.์›”๋ณ„๋งค์ถœ (ํŒŒ๋ž‘)๋งค์ถœ๋ˆ„๊ณ„ (์ฃผํ™ฉ) : ์ƒ๋Œ€์ ์œผ๋กœ ๋‹จ๊ธฐ์  ์ถ”์ด์ด๋™ํ•ฉ๊ณ„ (ํšŒ์ƒ‰) : ํ•ด๋‹น ์›” ๊ธฐ์ค€ 1๋…„์น˜ ๋ˆ„์ ํ•ฉ๊ณ„์žฅ๊ธฐ์  ์ถ”์ด๋ฅผ ์•Œ ์ˆ˜ ์žˆ์Œ๊ณ„์ ˆ์„ฑ, ํ”„๋กœ๋ชจ์…˜ ๋“ฑ ๋ณ€๋™์„ฑ์„ ์™„ํ™”ํ•ด ์ถ”์„ธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์ŒZ-์ฐจํŠธ ๊ทธ๋ฆฌ๊ธฐ (์‹ค์Šต)Sales_Transaction ํ…Œ์ด๋ธ”์—์„œ ํ˜ธ์ฃผ, ์ดํƒˆ๋ฆฌ์•„์˜ ์›”๋ณ„ ๋งค์ถœ,  ๋งค์ถœ ๋ˆ„๊ณ„ ๊ตฌํ•˜๊ธฐ ## 1. ํ˜ธ์ฃผ, ์ดํƒˆ๋ฆฌ์•„์˜ ์›”๋ณ„ ๋งค์ถœ, ๋งค์ถœ ๋ˆ„๊ณ„ ๊ตฌํ•˜๊ธฐ SELECT Country ,DATE_FORMAT(STR_TO_DATE(Date,'%m/%d/%Y'),'%Y-%m') AS dt_month ..
240604 Today I LearnPivot Table๐Ÿ’ก ํ”ผ๋ฒ— ํ…Œ์ด๋ธ”(pivot table)ํ‘œ์˜ ํ–‰๊ณผ ์—ด์„ ์ „ํ™˜ํ•˜๋Š” ๋“ฑ์˜ ๊ณผ์ •์„ ํ†ตํ•˜์—ฌ ํ†ต๊ณ„๋ฅผ ์žฌ์ •๋ ฌํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋กœ ํ‘œ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์•ฝํ•˜๋Š” ๋ฐฉ๋ฒ•. ์ด์—๋Š” ์ง‘๊ณ„ํ•จ์ˆ˜(ํ•ฉ๊ณ„, ํ‰๊ท  ๋“ฑ)๊ฐ€ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.SQL๋กœ Unpivot ํ•˜๊ธฐ1. ์ผ๋ จ ๋ณ€ํ˜ธ๋ฅผ ๊ฐ€์ง„ ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐSELECT 1 AS idx UNION ALL SELECT 2 AS idxUNION ALL SELECT 3 AS idx UNION ALL SELECT 4 AS idx2. ํ”ผ๋ฒ— ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ CROSS JOIN ํ•˜๊ธฐSELECT *FROM quarterly_sales CROSS JOIN (SELECT 1 AS idx UNION ALL SELECT 2 AS idx UNION..
240530 Today I Learn๋ฆฌํ…์…˜์ด๋ž€?๐Ÿ’ก ๋ฆฌํ…์…˜(Retention)์‹œ๊ฐ„์ด ์ง€๋‚ ์ˆ˜๋ก ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์œ ์ €๊ฐ€ ์ œํ’ˆ์œผ๋กœ ๋‹ค์‹œ ๋Œ์•„์˜ค๋Š”์ง€๋ฅผ ์ธก์ •ํ•œ ๊ฒƒ๊ณ ๊ฐ์˜ ์–ด๋–ค ํ–‰๋™์„ ๋ฆฌํ…์…˜์œผ๋กœ ์‚ผ์„ ๊ฒƒ์ธ์ง€๋Š” ์„œ๋น„์Šค๋งˆ๋‹ค ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.ํ”ํžˆ ์žฌ๋ฐฉ๋ฌธ, ์žฌ๊ตฌ๋งค, ๊ตฌ๋งค ํ›„ ๋กœ๊ทธ์ธ ๋“ฑ์œผ๋กœ ์ •์˜๋จ์„œ๋น„์Šค์˜ ๋ฐฉ๋ฌธ ๋นˆ๋„, ๊ตฌ๋งค ๋นˆ๋„, ๋„๋ฉ”์ธ ํŠน์„ฑ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์ •์˜๋ฆฌํ…์…˜์„ ์ •์˜ํ•  ๋•Œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ‘๊ทธ ํ–‰๋™์ด ์ •๋ง ๋น„์ฆˆ๋‹ˆ์Šค์—์„œ ์ค‘์š”ํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋Š” ๊ณ ๊ฐ์˜ ํ•ต์‹ฌ ํ–‰๋™์ธ๊ฐ€?’ ๋ผ๋Š” ๋ฌผ์Œ์ด๋‹ค.๋ฆฌํ…์…˜์˜ ์ข…๋ฅ˜1. ํด๋ž˜์‹ ๋ฆฌํ…์…˜ (Classic / N-Day)๊ณ ๊ฐ์ด A ํ–‰๋™์„ ์ฒ˜์Œ ํ•œ ๋‚ ์งœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ, ๋ช‡์ผ์ฐจ์— ๋‹ค์‹œ A ํ–‰๋™์„ ํ–ˆ๋Š”์ง€๋ฅผ ๋‚˜ํƒ€๋ƒ„.ํŽ˜์ด์Šค๋ถ, ์ธ์Šคํƒ€๊ทธ๋žจ, ์œ ํŠœ๋ธŒ, ํ‹ฑํ†ก, ์Šต๊ด€ ์•ฑ ๋“ฑ ๋งค์ผ๋งค์ผ ์ ‘์†ํ•˜๋Š” ์„œ๋น„์Šค์—์„œ ์‚ฌ์šฉํ•˜๋ฃจ ๋‹จ์œ„๋กœ ์ง‘๊ณ„ํ•˜๋Š” ..
240528 Today I Learnํผ๋„(Funnel)์ด๋ž€?๐Ÿ’ก ํผ๋„(Funnel)์ด๋ž€?์†Œ๋น„์ž์˜ ํ–‰๋™์„ ๊ธฐ์—… ๊ด€์ ์—์„œ ์žฌ๊ตฌ์„ฑํ•œ ๊ฒƒ์œผ๋กœ, ๊ณ ๊ฐ์ด ์œ ์ž…๋˜์–ด ์ „ํ™˜์— ์ด๋ฅด๋Š” ๋‹จ๊ณ„๋ฅผ ์ˆ˜์น˜๋กœ ํ™•์ธํ•˜๊ณ  ๋ถ„์„ํ•˜๋Š” ๋ฐฉ๋ฒ•๋ก → ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ ๊ณ ๊ฐ์ด ํผ๋„์˜ ๋๊นŒ์ง€ ํ†ต๊ณผํ•˜๋Š” ๊ฒƒ! ์ด๋ฅผ ์ „ํ™˜(conversion)/ Key Event ๋ผ๊ณ  ํ•œ๋‹ค.ํผ๋„์˜ ์ข…๋ฅ˜๋“ค1. ๋งˆ์ผ€ํŒ… ํผ๋„2.AARRR ํผ๋„3. ์‚ฌ์šฉ์ž ๊ทธ๋ฃน๋ณ„ ํผ๋„ ๋น„๊ต ํผ๋„ ๋‹จ๊ณ„ ์ˆ˜๋ฆฝํ•˜๊ธฐ๐Ÿ‘… ๋ง›๋ณด๊ธฐ ์˜ˆ์‹œ๋งŒ์•ฝ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์ŠคํŒŒ๋ฅดํƒ€ ์ฝ”๋”ฉ ํด๋Ÿฝ์˜ ๋ฐ์ดํ„ฐ ๋ถ„์„๊ฐ€์ด๊ณ , ์‹ ์ฒญ ๋งˆ๊ฐ์ผ์ด ๋‹ค๊ฐ€์™€ ์ผ์ฃผ์ผ ์•ˆ์— ๋ฐ์ดํ„ฐ ๋ถ„์„ ํŠธ๋ž™ 3๊ธฐ์˜ ์ฐธ๊ฐ€์ž ์ˆ˜๋ฅผ 5๋ฐฐ ์ด์ƒ ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ์‹ถ๋‹ค๋ฉด, ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”? โœ… ๋‚ด๊ฐ€ ์“ด ๋‹ต์•ˆ1. ํš๋“ - ์›น์‚ฌ์ดํŠธ ๋ฐฉ๋ฌธ, ๊ณต๊ณ  ์กฐํšŒ, ์ปค๋ฆฌํ˜๋Ÿผ ์กฐํšŒ2. ํ™œ์„ฑํ™” - ํšŒ์›๊ฐ€์ž…3..
240523 Today I Learn์ด๋™ ํ‰๊ท ์„ ์‚ฌ์šฉํ•œ ๋‚ ์งœ๋ณ„ ๋งค์ถœ ์ถ”์ด ๋ณด๊ธฐ์ด๋™ํ‰๊ท ์ด๋ž€ ๋ฌด์—‡์ผ๊นŒ? ์™œ ์‚ฌ์šฉํ• ๊นŒ?๐Ÿ’ก ์ด๋™ํ‰๊ท ๋ฒ•์ด๋™ ํ‰๊ท ๋ฒ•์€ ์ „์ฒด ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์˜ ์—ฌ๋Ÿฌ ํ•˜์œ„์ง‘ํ•ฉ์— ๋Œ€ํ•œ ์ผ๋ จ์˜ ํ‰๊ท ์„ ๋งŒ๋“ค์–ด ๋ฐ์ดํ„ฐ ์š”์†Œ๋ฅผ ๋ถ„์„ํ•˜๋Š” ๊ณ„์‚ฐ์ด๋‹ค.ROWS BETWEEN lower_bound AND upper_boundโ€‹`UNBOUNDED PRECEDING/ FOLLOWING`- ํ˜„์žฌ row ์•ž/๋’ค์˜ ๋ชจ๋“  row`n PRECEDING/ FOLLOWING` - ํ˜„์žฌ row ์•ž/๋’ค์˜ n๊ฐœ์˜ row`CURRENT ROW' - ํ˜„์žฌ row์ด๋™ํ‰๊ท  ๊ตฌํ•˜๊ธฐ  - 7์ผ ์ด๋™ํ‰๊ท  ๊ตฌํ•˜๊ธฐSELECT dt, SUM(purchase_amount), AVG(SUM(purchase_amount)) OVER(ORDER ..
240521 Today I LearnWindow Function๐Ÿ’ก Window Functionํ–‰๊ณผ ํ–‰์˜ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ์ œ๊ณต๋˜๋Š” ํ•จ์ˆ˜๋กœ ๋ชจ๋“  ์ปฌ๋Ÿผ์„ ์žƒ๊ณ  ์‹ถ์ง€ ์•Š์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.# ์œˆ๋„์šฐ ํ•จ์ˆ˜ ๊ธฐ๋ณธ๋ฌธ๋ฒ• SELECT WINDOW_FUNCTION () OVER([PARTITION BY ์ปฌ๋Ÿผ] [ORDER BY ์ปฌ๋Ÿผ])FROM ํ…Œ์ด๋ธ”๋ช…โ€‹  ์ข…๋ฅ˜ํ•จ์ˆ˜์ˆœ์œ„RANK, DENSE_RANK, ROW_NUMBER์ง‘๊ณ„SUM, MAX, MIN, AVG, COUNT์ˆœ์„œFIRST_VALUE, LAST_VALUE, LAG, LEAD๋น„์œจRATIO_TO_REPROT, CUME_DIST์ˆœ์œ„๐Ÿ’ก ROW_NUMBER' ์ค„์„ธ์šฐ๊ธฐ๋ฅผ ํ•œ๋‹ค.' →  ์ค„์„ธ์šฐ๊ธฐ๋ฅผ ํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ ์ค‘๋ณต ์ˆœ์œ„ ๋ฐœ์ƒ ๋ถˆ๊ฐ€ํ•จ.์–ด๋–ป๊ฒŒ๋“  ์ˆœ์œ„๋ฅผ ์ง€์ •ํ•จ.ROW_NUMBE..
ny:D
'๐Ÿ“’ Today I Learn/๐Ÿ˜Š My SQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก