aggregate function 이라고 불리는 것들이 있다.
흔히 group by와 함께 쓰이는 것들로 min/max/avg/sum 등 과 같은 것이다.
특징은 다음과 같다
- null 값은 무시
- 주로 여러줄 -> 한줄로 만듦
- 숫자(sum/avg 등 함수의 결과)는 decimal(정수)/double(소수)로 리턴함
- 자동 캐스팅 함수가 있음(sum/avg는 number로)
기타 함수 별 특징은 아래 사이트를 확인하자.
https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html
MySQL :: MySQL 8.0 Reference Manual :: 12.20.1 Aggregate Function Descriptions
MySQL 8.0 Reference Manual / ... / Functions and Operators / Aggregate Functions / Aggregate Function Descriptions 12.20.1 Aggregate Function Descriptions This section describes aggregate functions that operate on sets of values. They are
dev.mysql.com
그에 비해 window function은
- 각 줄별로 계산 결과를 포함한다.(축약된 데이터가 아니라 원래 row에 추가해서 보여줄 수 있음)
- 대부분의 aggregate function을 그대로 사용 가능.
- 그 외 순위, 순서, 비율 등과 관련된 함수도 있음.
- group by 사용 불가
역시나 자세한 설명은 공식 문서에서.
https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html
MySQL :: MySQL 8.0 Reference Manual :: 12.21.1 Window Function Descriptions
12.21.1 Window Function Descriptions This section describes nonaggregate window functions that, for each row from a query, perform a calculation using rows related to that row. Most aggregate functions also can be used as window functions; see Section 12
dev.mysql.com
기본 모양새
함수(컬럼) over (partition by 컬럼 order by 컬럼)
데이터 위치 바꾸기
- lag 데이터를 밀고
- lead는 데이터를 당김
참고
좋은 설명.. https://moonpiechoi.tistory.com/128
[SQL] 윈도우 함수 (WINDOW FUNCTION)
WINDOW FUNCTION 개요 행과 행 간의 관계를 쉽게 정의하기 위해 만든 함수가 윈도우 함수다. 윈도우 함수는 분석 함수나 순위 함수로도 알려져 있다. 윈도우 함수는 기존에 사용하던 집계 함수도 있
moonpiechoi.tistory.com
'개발 > sql' 카테고리의 다른 글
[mysql] 유저의 등수 구하기 rank under v8 (0) | 2024.02.06 |
---|---|
[DB] 분산환경에서 데이터 저장소 선택과 활용 (0) | 2023.07.24 |
[형상관리] flyway vs liquibase (0) | 2022.07.08 |
[mysql] jsonpath (0) | 2022.05.27 |
[sql] case vs if (0) | 2022.05.02 |