DATE_TRUNC
지정된 날짜 기준으로 값을 자르고 타임스탬프(timestamp) 또는 간격(interval)을 반환하는 함수입니다.
예제
여기에 아래와 같은 금액 테이블(amounts)이 있다고 가정하시고 보시면 되겠습니다.
날짜 (컬럼명: trade_date / 타입:date) | 금액 (컬럼명: amount / 타입:bigint) |
2023-04-26 | 100,000 |
연간 합산 평균
SELECT
date_trunc( 'year', trade_date), sum(amount), avg(amount)
FROM amounts
GROUP BY date_trunc( 'year', trade_date)
월간 합산 평균
SELECT
date_trunc( 'month', trade_date), sum(amount), avg(amount)
FROM amounts
GROUP BY date_trunc( 'month', trade_date)
주간 합산 평균
SELECT
date_trunc( 'week', trade_date), sum(amount), avg(amount)
FROM transactions
GROUP BY date_trunc( 'week', trade_date)
참조
[PostgreSQL date_trunc]: https://www.postgresqltutorial.com/postgresql-date-functions/postgresql-date_trunc/
PostgreSQL date_trunc Function By Examples
Summary: this tutorial shows you how to use the PostgreSQL date_trunc() function to truncate a timestamp or interval to a specified level of precision. Introduction to the PostgreSQL date_trunc function The date_trunc function truncates a TIMESTAMP or an
www.postgresqltutorial.com
728x90
'Database > PostgreSQL' 카테고리의 다른 글
[PostgreSQL] 가장 최근 날짜, 점수별 건수, 점수 평균 구하기 (0) | 2023.05.03 |
---|---|
[PostgreSQL] 다중 컬럼 평균 구하기 (Average Multiple Columns) (0) | 2023.04.28 |
[PostgreSQL] COALESCE null to 0 (0) | 2023.04.24 |
[PostgreSQL] DATE_PART 사용하기 (0) | 2023.04.13 |
[PostgreSQL] 월 차이 계산 (0) | 2023.04.13 |
댓글