ANSI SQL Group By
ANSI SQL Group By
ANSI SQL Group By
- ANSI SQL에서는 동일한 값을 가진 컬럼을 기준으로 그룹별 연산을 적용하기 위해
group by
구문을 사용 Having
구문을 통해 특정 조건을 만족하는 그룹만 볼 수 있음- ANSI SQL에서는
group by
,having
절에서select
절의 alias를 사용 불가- 이는
group by
,having
등이select
보다 먼저 실행되기 때문 - MySQL에서는
having
절에서 사용을 허락하지만, 이는 SQL 표준이 아님
- 이는
- 다음과 같이 사용
SELECT userType, count(*)
FROM users
GROUP BY userType
HAVING COUNT(userType) > 5;
References
- https://leejinseop.tistory.com/23
- https://stackoverflow.com/questions/14048672/why-cant-i-use-an-alias-for-an-aggregate-in-a-having-clause
- https://stackoverflow.com/questions/49888360/using-alias-in-the-where-and-having-statements