SELECT
PRODUCT_ID,
--주문 수
count (distinct order_id) as order_cnt,
--총 상품수량
sum(quantity) as sum_quan,
--총 금액
sum(unit_price*quantity) as total_price,
--주문건당 거래액
sum(unit_price*quantity)/count(distinct order_id) as price_by_ord,
FROM `ls-data-literacy-410915.database.co_order_items`
group by PRODUCT_ID
--주문건당 평균 거래액이 100이상인 상품 ID 확인하기
having sum(unit_price*quantity)/count(distinct order_id) >= 100
--원칙상 별칭은 having에서 사용안됨 (아직 별칭 정의안됨 - 데이터 조회 순서에 따라)
2. ORDER BY | ~의 순서대로
| 나열하느라 데이터 과부하 걸릴 수 있음...ㅜ 추천하지는 않음. 구글스프레드시트에서 쓰는 것 추천
테이블을 특정 컬럼의 값을 기준으로 정렬하기 위한 구문
여러 개의 칼럼을 기준으로 정렬할 수 있음 (1. 단일칼럼 2. 복수 칼럼 모두 정렬 가능)
SELECT
PRODUCT_ID,
--주문 수
count (distinct order_id) as order_cnt,
--총 상품수량
sum(quantity) as sum_quan,
--총 금액
sum(unit_price*quantity) as total_price,
--주문건당 거래액
sum(unit_price*quantity)/count(distinct order_id) as price_by_ord,
FROM `ls-data-literacy-410915.database.co_order_items`
group by PRODUCT_ID
--order by는 select 뒤에 읽히기 때문에 별칭 써도 됨.
order by price_by_ord desc
limit 3
내림차순 desc, 상위 3개만 보고 싶으므로 limit 3
3. 오늘의 팥앙금🥮
select와 from은 필수 구문, 그 외에는 선택구문이다.
테이블 자체를 줄인다면 WHERE, 산출된 결과물에서 데이터를 뽑는다면 HAVING 이다.
별칭까지 마무리된 결과를 단순 순서만 변경한다면 ORDER BY 로 오름차순/내림차순 설정하면 된다.