https://solvesql.com/problems/weekday-stats-airpollution/
https://solvesql.com/problems/weekday-stats-airpollution/
solvesql.com
🔍 문제
- `measurements` 테이블에서 `measured_at` 컬럼에 따라서 날짜별 대기오염 측정 지수가 존재한다.
- 각 요일별 평균 대기오염 측정 지수를 구하면 된다.
🎯정답
select
(case extract(isodow from measured_at)
when '1' then '월요일'
when '2' then '화요일'
when '3' then '수요일'
when '4' then '목요일'
when '5' then '금요일'
when '6' then '토요일'
when '7' then '일요일'
END) as "weekday",
round(avg(no2), 4) as "no2",
round(avg(o3), 4) as "o3",
round(avg(co), 4) as "co",
round(avg(so2), 4) as "so2",
round(avg(pm10), 4) as "pm10",
round(avg(pm2_5), 4) as "pm2_5"
from measurements
group by extract(isodow from measured_at)
order by extract(isodow from measured_at);
❓풀이
- postgresql 기준으로 날짜에서 요일을 추출하는 방법은 다음과 같다.
- `extract(isodow from '날짜컬럼')`
- 월요일 : 1, 화요일 : 2, 수요일 : 3, 목요일 : 4, 금요일 : 5, 토요일 : 6, 일요일 : 7 으로 매칭된다
- 월요일, 화요일 처럼 출력 결과를 나타내야 하기 때문에 case when 구문을 사용해서 변경해줬다.
'프로그래밍 언어 > 02. SQL' 카테고리의 다른 글
| [PostgreSQL] solvesql LV.3 멀티 플랫폼 게임 찾기 (0) | 2025.10.21 |
|---|---|
| [PostgreSQL] solvesql LV.3 폐쇄할 따릉이 정류소 찾기 2 (0) | 2025.10.15 |
| [PostgreSQL] solvesql LV.3 온라인 쇼핑몰의 월 별 매출액 집계 (0) | 2025.10.13 |
| [PostgreSQL] solvesql LV.3 멘토링 짝꿍 리스트 (0) | 2025.10.12 |
| [PostgreSQL] solvesql LV.3 쇼핑몰의 일일 매출액과 ARPPU (0) | 2025.10.12 |
