할부는 몇 개월로 해드릴까요 : solvesql

오유찬·2025년 12월 18일

SQL

목록 보기
21/71
SELECT  
  payment_installments, COUNT(*) as order_count, 
  MIN(payment_value) as min_value, 
  MAX(payment_value) as max_value, 
  AVG(payment_value) as avg_value
FROM    
  olist_order_payments_dataset
GROUP BY
  payment_installments

→ ❎ 틀림.

WHERE payment_type = 'credit_card' 조건 빼먹지 말고 체크할 것!
하지만 여전히 정답과는 결과가 다르다.
틀린 게 없어보이는데 틀렸으니까 테이블의 컬럼을 다시 한 번 자세히 살펴보자.

answer

SELECT  
  payment_installments, 
  COUNT(DISTINCT order_id) as order_count, 
  MIN(payment_value) as min_value, 
  MAX(payment_value) as max_value, 
  AVG(payment_value) as avg_value
FROM    
  olist_order_payments_dataset
WHERE 
  payment_type = 'credit_card'
GROUP BY
  payment_installments

중복되는 주문 ID가 있을 것 같아서 DISTINCT를 추가했는데 그게 정답이었다.

SELECT  payment_installments, COUNT(order_id) as order_count
FROM    olist_order_payments_dataset
GROUP BY payment_installments
HAVING count(order_id) >= 2

실제로 중복을 체크해보면 이를 확인할 수 있다.

profile
열심히 하면 재밌다

0개의 댓글