예시 테이블 구조 ( 테이블명: scores )
학생이름 (컬럼명: student_name / 타입: varchar) |
점수 (컬럼명: score / 타입: smallint) | 거래일자 (컬럼명: exam_date / 타입: date) |
홍길동 | 90 | 2023-04-28 |
최근날짜 기준 1~50 건수, 50 ~ 100 건수, 점수 평균
WITH count_scores AS (
SELECT
COUNT(CASE WHEN score > 1 AND total_score <= 50 THEN id END) AS a1b50,
COUNT(CASE WHEN score > 50 AND total_score <= 100 THEN id END) AS a50b100,
FLOOR(AVG(score)) AS average
FROM scores
WHERE (student_name, exam_date)
IN (
SELECT student_name, MAX(exam_date) AS exam_date
FROM scores
GROUP BY student_name
)
)
SELECT * FROM count_scores;
728x90
'Database > PostgreSQL' 카테고리의 다른 글
[PostgreSQL] 현재 시퀀스 마지막 값 조회하기 (0) | 2023.06.05 |
---|---|
[PostgreSQL] Update Multiple Columns From Select (0) | 2023.05.09 |
[PostgreSQL] 다중 컬럼 평균 구하기 (Average Multiple Columns) (0) | 2023.04.28 |
[PostgreSQL] DATE_TRUNC 활용 (연간, 월간, 주간 / 합산, 평균) (0) | 2023.04.26 |
[PostgreSQL] COALESCE null to 0 (0) | 2023.04.24 |
댓글