Group by절
집계함수
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
SQL 기본문법 4 (0) | 2022.08.01 |
---|---|
SQL 기본문법 1 (0) | 2022.07.12 |