SELECT daily_stats.week
, AVG(daily_stats.incidents_daily)
FROM (
SELECT week
, date
, COUNT(incident_id) AS incidents_daily
FROM crimes
GROUP BY week, date
) daily_stats
GROUP BY daily_stats.week
1. WHERE 절 서브쿼리 결과물이 1개일 때
=
사용SELECT *
FROM crimes
WHERE date = (SELECT MIN(date) FROM crimes)
2. WHERE 절 서브쿼리 결과물이 여러 개일 때
IN
또는 OR
사용SELECT *
FROM crimes
WHERE date IN (SELECT DISTINCT date FROM crimes ORDER BY date DESC LIMIT 5)