๐Ÿ’ SQL/Group By

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค][SQL] ๋Œ€์—ฌ ํšŸ์ˆ˜๊ฐ€ ๋งŽ์€ ์ž๋™์ฐจ๋“ค์˜ ์›”๋ณ„ ๋Œ€์—ฌ ํšŸ์ˆ˜ ๊ตฌํ•˜๊ธฐ

์„ ๋‹ฌ 2023. 5. 31. 23:00
๋ฐ˜์‘ํ˜•

https://school.programmers.co.kr/learn/courses/30/lessons/151139

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๋ฌธ์ œ

1. CAR_RENTAL_COMPANY_RENTAL_HISTORY ํ…Œ์ด๋ธ”์—์„œ

2. ๋Œ€์—ฌ ์‹œ์ž‘์ผ์„ ๊ธฐ์ค€์œผ๋กœ 2022๋…„ 8์›”๋ถ€ํ„ฐ 2022๋…„ 10์›”๊นŒ์ง€ ์ด ๋Œ€์—ฌ ํšŸ์ˆ˜๊ฐ€ 5ํšŒ ์ด์ƒ์ธ ์ž๋™์ฐจ๋“ค์— ๋Œ€ํ•ด์„œ

3. ํ•ด๋‹น ๊ธฐ๊ฐ„ ๋™์•ˆ์˜ ์›”๋ณ„ ์ž๋™์ฐจ ID ๋ณ„ ์ด ๋Œ€์—ฌ ํšŸ์ˆ˜(์ปฌ๋Ÿผ๋ช…: RECORDS) ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

4. ๊ฒฐ๊ณผ๋Š” ์›”์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ , ์›”์ด ๊ฐ™๋‹ค๋ฉด ์ž๋™์ฐจ ID๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

5. ํŠน์ • ์›”์˜ ์ด ๋Œ€์—ฌ ํšŸ์ˆ˜๊ฐ€ 0์ธ ๊ฒฝ์šฐ์—๋Š” ๊ฒฐ๊ณผ์—์„œ ์ œ์™ธํ•ด์ฃผ์„ธ์š”.

 

์ฝ”๋“œ

-- ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
select month(start_date) as month, car_id, count(history_id) as records
from car_rental_company_rental_history
where car_id in (
    select car_id
    from car_rental_company_rental_history
    where start_date between '2022-08-01' and '2022-10-31'
    group by car_id having count(history_id) >= 5)
    and start_date between '2022-08-01' and '2022-10-31'
group by month, car_id having count(history_id) != 0
order by month asc, car_id desc

 

ํ’€์ด

์•„๋ž˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ž๋™์ฐจ๋“ค์„ ๋ฝ‘์•„๋ƒ…๋‹ˆ๋‹ค.

2. ๋Œ€์—ฌ ์‹œ์ž‘์ผ์„ ๊ธฐ์ค€์œผ๋กœ 2022๋…„ 8์›”๋ถ€ํ„ฐ 2022๋…„ 10์›”๊นŒ์ง€ ์ด ๋Œ€์—ฌ ํšŸ์ˆ˜๊ฐ€ 5ํšŒ ์ด์ƒ์ธ ์ž๋™์ฐจ๋“ค์— ๋Œ€ํ•ด์„œ
    select car_id
    from car_rental_company_rental_history
    where start_date between '2022-08-01' and '2022-10-31'
    group by car_id having count(history_id) >= 5

 

๋ฝ‘์•„๋‚ธ ์ž๋™์ฐจ์— ํ•ด๋‹นํ•˜๋Š” ๊ธฐ๋ก๋“ค์„ ๋ฝ‘์•„๋ƒ…๋‹ˆ๋‹ค

์ด๋•Œ ๋‚ ์งœ ์กฐ๊ฑด์„ ํ•œ๋ฒˆ ๋” ๊ฑธ์–ด์ค˜์•ผ ํ•ด๋‹น ๊ธฐ๊ฐ„์— ์†ํ•˜๋Š” ๊ธฐ๋ก์ด ๋ฝ‘ํž™๋‹ˆ๋‹ค (์—†์œผ๋ฉด ๋ชจ๋“  ๋‚ ์งœ์—์„œ ๋ฝ‘์•„๋ƒ„)

select *
from car_rental_company_rental_history
where car_id in (
    select car_id
    from car_rental_company_rental_history
    where start_date between '2022-08-01' and '2022-10-31'
    group by car_id having count(history_id) >= 5)
    and start_date between '2022-08-01' and '2022-10-31'

 

์ด์ œ ์›”๋ณ„๋กœ, ์ฐจ๋ณ„๋กœ ๊ทธ๋ฃนํ™”ํ•˜๊ณ  ์›”๊ณผ ์ฐจ์™€ ๊ธฐ๋ก์„ ์ถœ๋ ฅํ•ฉ์‹œ๋‹ค (๋ฌธ์ œ 3๋ฒˆ, 5๋ฒˆ ์กฐ๊ฑด)

3. ํ•ด๋‹น ๊ธฐ๊ฐ„ ๋™์•ˆ์˜ ์›”๋ณ„ ์ž๋™์ฐจ ID ๋ณ„ ์ด ๋Œ€์—ฌ ํšŸ์ˆ˜(์ปฌ๋Ÿผ๋ช…: RECORDS) ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
5. ํŠน์ • ์›”์˜ ์ด ๋Œ€์—ฌ ํšŸ์ˆ˜๊ฐ€ 0์ธ ๊ฒฝ์šฐ์—๋Š” ๊ฒฐ๊ณผ์—์„œ ์ œ์™ธํ•ด์ฃผ์„ธ์š”.
select month(start_date) as month, car_id, count(history_id) as records
from car_rental_company_rental_history
where car_id in (
    select car_id
    from car_rental_company_rental_history
    where start_date between '2022-08-01' and '2022-10-31'
    group by car_id having count(history_id) >= 5)
    and start_date between '2022-08-01' and '2022-10-31'
group by month, car_id having count(history_id) != 0

 

์ˆœ์„œ๋„ ์žŠ์ง€ ๋งˆ์„ธ์š”! (4๋ฒˆ ์กฐ๊ฑด)

4. ๊ฒฐ๊ณผ๋Š” ์›”์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ , ์›”์ด ๊ฐ™๋‹ค๋ฉด ์ž๋™์ฐจ ID๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.
select month(start_date) as month, car_id, count(history_id) as records
from car_rental_company_rental_history
where car_id in (
    select car_id
    from car_rental_company_rental_history
    where start_date between '2022-08-01' and '2022-10-31'
    group by car_id having count(history_id) >= 5)
    and start_date between '2022-08-01' and '2022-10-31'
group by month, car_id having count(history_id) != 0
order by month asc, car_id desc
๋ฐ˜์‘ํ˜•