티스토리 뷰

반응형

 

대표값

산술평균, 기하평균, 중앙값 등이 있다.

 

문자열함수

FORMAT(X,D) X형식 '#,###,###.##'를 D자리수로 맞춘다.

 

select C.USERNAME 이름,
count(*) 주문량,
format(sum(saleprice),0) 합계,
format(avg(saleprice),1) 평균,
MAX(saleprice) 최대,
MIN(saleprice) 최소
from orders O
left join customer C
on O.custid=c.custid
group by 이름;

 

Partition by 

그룹핑해서 순위를 매기고자 할 때 쓴다.

 

with rollup

group by adress with rollup

 

NULL값도 출력이 된다.

 

having구문을 사용하면 null값을 제거할 수 있다.

 

순위 매기기

select B.bookname, Rank() over(order by O.saleprice) as RANKING
from book B, orders O

 

Partition by

select B.bookname, custid, row_number() over(order by O.saleprice) as RANKING
from book B, orders O
where B.bookid=O.bookid
group by 1;

아이디와 무관하게

그냥 순위가 매겨진다.

select B.bookname, row_number() over(partition by O.custid order by O.saleprice) as RANKING
from book B, orders O
where B.bookid=O.bookid
group by 1;

 

각각의 id별로 1,2,3등을 나눈다.

 

 

rollup

group by 묶은 것에 대해 개별 소계를 내주는 함수

select substring(address,1,12) as 지역, bookname, count(*) 수량
from customer C, Book B, orders O
where O.bookid=B.bookid and C.custid=O.custid
group by address with rollup
order by username asc, bookname desc;

위에는 rollup이 있을 때, 아래는 없을 때이다. rollup은 철학의 역사가 16개 있는 소계를 보여준다.

 

반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함
반응형