SQL
SQL 기본문법 3
2^7
2022. 8. 1. 10:04
Group by절
집계함수
- sum() : 합계
- avg() : 평균
- min() : 최소값
- max() : 최대값
- count() : 행의 개수
- count(distinct) : 행의 개수(중복은 1개만 인정)
select mem_id, sum(amount) from buy group by mem_id;
select mem_id "회원 아이디", sum(amount) "총 구매 개수" from buy group by mem_id;
-- 별칭 사용
select mem_id "회원 아이디", sum(price*amount) "총 구매 금액" from buy group by mem_id;
select avg(amount) "평균 구매 개수" from buy;
select mem_id, avg(amount) "평균 구매 개수" from buy group by mem_id;
select count(phone1) "연락처가 있는 회원" from member;
having 절
select mem_id "회원 아이디", sum(price*amount) " 총 구매 금액" from buy group by mem_id;
select mem_id "회원 아이디", sum(price*amount) " 총 구매 금액"
from buy
group by mem_id
having sum(price*amount) > 1000;
select mem_id "회원 아이디", sum(price*amount) " 총 구매 금액"
from buy
group by mem_id
having sum(price*amount) > 1000
order by sum(price*amount) desc;
참조 : 혼자 공부하는 SQL
728x90