๐Ÿ’ SQL/Group By

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

์„ ๋‹ฌ 2023. 6. 9. 23:45
๋ฐ˜์‘ํ˜•

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

 

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

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

programmers.co.kr

 

USER_INFO ํ…Œ์ด๋ธ”๊ณผ ONLINE_SALE ํ…Œ์ด๋ธ”์—์„œ

๋…„, ์›”, ์„ฑ๋ณ„ ๋ณ„๋กœ

์ƒํ’ˆ์„ ๊ตฌ๋งคํ•œ ํšŒ์›์ˆ˜๋ฅผ ์ง‘๊ณ„ํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

๊ฒฐ๊ณผ๋Š” ๋…„, ์›”, ์„ฑ๋ณ„์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

์ด๋•Œ, ์„ฑ๋ณ„ ์ •๋ณด๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๊ฒฐ๊ณผ์—์„œ ์ œ์™ธํ•ด์ฃผ์„ธ์š”.

 

select year(sales_date) as year, month(sales_date) as month, gender, 
    count(distinct s.user_id) as users
from user_info i join online_sale s on i.user_id = s.user_id
where gender is not null
group by year, month, gender
order by year, month, gender
๋ฐ˜์‘ํ˜•