์ „์ฒด ๊ธ€ 47

[SQL] leetcode | 550. Game Play Analysis IV ์˜ˆ์ œ ํ’€์ด

๋ฌธ์ œhttps://leetcode.com/problems/game-play-analysis-iv/ ์ฝ”๋“œ๋‚ด ๋‹ต์•ˆ# Write your MySQL query statement belowwith lag_date as (select *, min(event_date) over (partition by player_id) as first_datefrom activity)select round(count(distinct player_id) / (select count(distinct player_id) from activity), 2) as fractionfrom lag_datewhere datediff(event_date, first_date) = 1-- 10๋ถ„ ํƒ€ ๋ธ”๋กœ๊ทธ ๋‹ต์•ˆSELECT ROUN..

Tools/- SQL 2025.01.23

[SQL] leetcode | 601. Human Traffic of Stadium ์˜ˆ์ œ ํ’€์ด | ์–ด๋–ค ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํ–‰์ด ์—ฐ์†์ ์œผ๋กœ n๊ฐœ

๋ฌธ์ œhttps://leetcode.com/problems/human-traffic-of-stadium/description/  WITH result AS ( SELECT id, visit_date, people, LAG(people) OVER (ORDER BY id) AS prev_people FROM stadium),session_marker AS ( SELECT id, visit_date, people, (CASE WHEN prev_people IS NULL THEN 1 WHEN prev_people = 3)ORDER BY session_gr..

Tools/- SQL 2025.01.19

[SQL] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์˜ˆ์ œ | ์ƒํ’ˆ์„ ๊ตฌ๋งคํ•œ ํšŒ์› ๋น„์œจ ๊ตฌํ•˜๊ธฐ

๋ฌธ์ œhttps://school.programmers.co.kr/learn/courses/30/lessons/131534 ํ”„๋กœ๊ทธ๋ž˜๋จธ์ŠคSW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„programmers.co.kr ์ฝ”๋“œselect year(sales_date) as year, month(sales_date) as month, count(distinct user_id) as purchased_users, round(count(distinct user_id) / (select count(*) from user_info where year(joined) = 2021), 1) as purchased_ratiofrom online_sal..

Tools/- SQL 2025.01.19

[SQL] leetcode | 262. Trips and users ์˜ˆ์ œ ํ’€์ด

๋ฌธ์ œhttps://leetcode.com/problems/trips-and-users/ ์ฝ”๋“œ ๋‹ต์•ˆwith result as (select id, client_id, driver_id, status, request_atfrom trips tjoin (select users_id, banned from users where role = 'client') con t.client_id = c.users_idjoin (select users_id, banned from users where role = 'driver') don t.driver_id = d.users_idwhere c.banned = 'No' and d.banned = 'No' ),request_rate as (select request_at as..

Tools/- SQL 2025.01.19

[SQL] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์˜ˆ์ œ | ๋ฌผ๊ณ ๊ธฐ ์ข…๋ฅ˜ ๋ณ„ ๋Œ€์–ด ์ฐพ๊ธฐ

๋ฌธ์ œhttps://school.programmers.co.kr/learn/courses/30/lessons/293261 ํ”„๋กœ๊ทธ๋ž˜๋จธ์ŠคSW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„programmers.co.kr ์ฝ”๋“œwith rank_info as ( select id, fish_type, length, rank() over (partition by fish_type order by length desc) as ranking from fish_info)select r.id, n.fish_name, lengthfrom rank_info rjoin fish_name_info non r.fish_type = n.fish_typew..

Tools/- SQL 2025.01.17

[SQL] solvesql ์„ธ์…˜ ์žฌ์ •์˜ํ•˜๊ธฐ | lag () over (), ์‹œ๊ฐ„ ์ฐจ์ด ๊ตฌํ•˜๊ธฐ

๋ฌธ์ œhttps://solvesql.com/problems/redefine-session/ https://solvesql.com/problems/redefine-session/ solvesql.com ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋‚ด์šฉ์กฐ์ฐจ ์ดํ•ด๊ฐ€ ์•ˆ๋๋‹คevent_name์ด session_start์ธ ํ–‰๋™์˜ ์‹œ๊ฐ„์ด๋ž‘ (+ 1์‹œ๊ฐ„)์ธ ์‹œ๊ฐ„์„ ๊ตฌํ•˜๋ฉด ๋˜๋Š” ๊ฑฐ ์•„๋‹Œ๊ฐ€?์ƒ๊ฐํ–ˆ๋Š”๋ฐํ•ด์„ค์„ ๋ณด๋‹ˆ๊นŒ ์™„์ „ ๋‹ค๋ฅด๊ฒŒ ํ’€์–ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. event๋ณ„๋กœ ์‹œ๊ฐ„์ฐจ๊ฐ€ 1์‹œ๊ฐ„ ์ด์ƒ์ธ ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ์„ธ์…˜์ธ ๊ฑธ๋กœ ์ •์˜ํ•ด์„œ ๊ฐ ์„ธ์…˜์˜ ์‹œ์ž‘๊ณผ ๋์„ ๊ตฌํ•˜๋Š” ๊ฑฐ์˜€๋‹ค.event_name์ด session_start์ธ ๊ฑฐ๋Š” ์‹ค์ œ ์„ธ์…˜๊ณผ๋Š” ์ƒ๊ด€์ด ์—†๋‹ค. ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๊ฑธ ์ดํ•ดํ–ˆ์–ด๋„ ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผํ• ์ง€ ๊ฐ๋„ ์•ˆ์™”์„ ๊ฒƒ ๊ฐ™๋‹ค.๋ ˆ์ฝ”๋“œ๋ณ„ ์‹œ๊ฐ„์ฐจ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋งŽ๊ณ  ์‹ค์ œ๋กœ ๋‚ด..

Tools/- SQL 2025.01.16

[SQL] solvesql ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ๋งค์ถœ ๋น„์œจ | sum() over ()

๋ฌธ์ œhttps://solvesql.com/problems/revenue-pct-per-category/ https://solvesql.com/problems/revenue-pct-per-category/ solvesql.com ์ฝ”๋“œgpt๋ฅผ ์กฐ์ ธ์„œ ์–ป์–ด๋‚ธ ๋‹ต์•ˆwith all_sale as ( -- ์„œ๋ธŒ ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„๋กœ ๋งค์ถœ์•ก์„ ๊ตฌํ•œ๋‹ค select category, sub_category, sum(sales) as sales_sub_category from records group by category, sub_category),sales_with_totals as ( -- ์„œ๋ธŒ ์นดํ…Œ๊ณ ๋ฆฌ, ์นดํ…Œ๊ณ ๋ฆฌ, ์ด ๋งค์ถœ์•ก์„ ๊ตฌํ•œ๋‹ค. select category, sub_categ..

Tools/- SQL 2025.01.16

[SQL] solvesql ๊ฐ€๊ตฌ ํŒ๋งค์˜ ๋น„์ค‘์ด ๋†’์•˜๋˜ ๋‚  ์ฐพ๊ธฐ

๋ฌธ์ œhttps://solvesql.com/problems/day-of-furniture/ https://solvesql.com/problems/day-of-furniture/ solvesql.com ์ฝ”๋“œ ์ „๋ฌธ1์ฐจ ์‹œ๋„with calc as ( SELECT order_date, count(*) AS total_orders, count( CASE WHEN category = 'Furniture' THEN 1 ELSE 0 END ) AS furniture_orders FROM records GROUP BY order_date )select order_date, furniture..

Tools/- SQL 2025.01.16

[MySQL] Case๋ฌธ | ๋ฌธ๋ฒ•๊ณผ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ์‚ดํŽด๋ณด๊ธฐ

๐Ÿ“Œ Case๋ฌธ ์—ฌ๋Ÿฌ ์กฐ๊ฑด์„ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ '๋‹ค์ค‘ ๋ถ„๊ธฐ' ๋ฌธ๋ฒ•์ด๋‹ค ์Šคํ† ์–ด๋“œ ํ”„๋กœ์‹œ์ €์—์„œ SQL ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋ฌธ์—์„œ ํ™œ์šฉ SELECT ๋ฌธ์—์„œ CASE๋ฌธ์œผ๋กœ ๊ฐ’ ํ˜ธ์ถœ CASE WHEN ์กฐ๊ฑด1 THEN SET ๋ณ€์ˆ˜๊ฐ’ WHEN ์กฐ๊ฑด1 THEN SET ๋ณ€์ˆ˜๊ฐ’ WHEN ์กฐ๊ฑด1 THEN SET ๋ณ€์ˆ˜๊ฐ’ ELSE SET ๋ณ€์ˆ˜๊ฐ’ END CASE; โœ๏ธ SELECT๋ฌธ์—์„œ CASE๋ฌธ ํ™œ์šฉํ•ด์„œ ํšŒ์›๋“ฑ๊ธ‰ ๋ถ„๋ฅ˜ํ•˜๊ธฐ SELECT M.mem_id, M.mem_name, SUM(price*amount) "์ด๊ตฌ๋งค์•ก", CASE WHEN (SUM(price*amount) >= 1500) THEN '์ตœ์šฐ์ˆ˜๊ณ ๊ฐ' WHEN (SUM(price*amount) >= 1000) THEN '์šฐ์ˆ˜๊ณ ๊ฐ' WHEN (SUM(price*amount) >= ..

Tools/- SQL 2024.04.07