728x90
쿼리를 사용시 Group by를 특정컬럼에 부분적으로 사용해야 할 필요가 있을 때가 있는데 집계함수와 partition by를 이용하면 손쉽게 특정컬럼에 Group by처리를 할 수 있습니다.
Example Data
집계함수의 Partition by
sum, avg, count.. 등 집계함수의 기능은 기존에 쓰는 기능이랑 동일하며 partition by로 chef를 묶은 다음 그 항목에 대해 집계함수를 처리합니다.
테이블 전체를 조회해서 Group by 처리 및 서브쿼리로 쓰기보단 아래의 방법으로 Group by처리를 유용하게 쓸 수 있습니다.
select *
, sum(score) over(partition by chef) as summary
, avg(score) over(partition by chef) as score_average
, count(score) over(partition by chef) as chef_food_count
, min(score) over(partition by chef) as min_score
, max(score) over(partition by chef) as max_score
from dbo.tmp_class
order by chef, score
728x90
'DataBase > MSSQL' 카테고리의 다른 글
SQL - (MSSQL) CONCAT_WS의 사용법 / 컬럼 합치기 (0) | 2021.08.18 |
---|---|
DB - (MSSQL) 날짜 변환처리 방법 (0) | 2021.07.14 |
댓글