susinlee 님의 블로그
Teams Power Users 본문
[문제]
https://datalemur.com/questions/teams-power-users
Microsoft SQL Interview Question | DataLemur
Microsoft SQL Interview Question: Write a query to retrieve the top 2 Power Users who sent the highest number of messages.
datalemur.com
[풀이]
- 날짜를 비교할 때 TO_CHAR()를 이용하여 'YYYY-MM' 형식 변환 및 비교가 가능
- TO_DATE(컬럼명, 'MM/DD/YYYY') 을 통해 'YYYY-MM-DD' 형태로 변환할 수 있음 (DATE타입으로 변경)
1. 2022-08에 메세지를 보낸 직원을 필터링한다
2. 직원 아이디별로 메세지 개수를 세어주고 메세지 개수로 내림차순 정렬 후 상위 2명만 뽑아낸다
[코드]
SELECT
sender_id
, COUNT(message_id) as message_count
FROM messages
WHERE TO_CHAR(sent_date, 'YYYY-MM') = '2022-08'
GROUP BY sender_id
ORDER BY message_count DESC
LIMIT 2
'코드카타 > SQL, Pandas' 카테고리의 다른 글
Top 5 Artists (0) | 2025.03.08 |
---|---|
Top Three Salaries (0) | 2025.03.08 |
Average Post Hiatus (Part 1) (0) | 2025.03.08 |
Page With No Likes (0) | 2025.03.07 |
Histogram of Tweets (0) | 2025.03.07 |