SELECT *
FROM (
SELECT B.USER_ID, B.NICKNAME, sum(A.price) TOTAL_SALES
FROM USED_GOODS_BOARD A left join USED_GOODS_USER B on A.WRITER_ID = B.USER_ID
WHERE STATUS = 'DONE'
GROUP BY B.USER_ID
) A
WHERE TOTAL_SALES >= 700000
ORDER BY TOTAL_SALES asc
where 절에는 SUM(A.PRICE)
이런 함수를 못 쓴다.
그래서 HAVING을 쓰면 된다!! 그룹화한 결과에서 조건을 넣을 때 사용할 수 있다!!
SELECT B.USER_ID, B.NICKNAME, SUM(A.PRICE) AS TOTAL_SALES
FROM USED_GOODS_BOARD A JOIN USED_GOODS_USER B ON A.WRITER_ID = B.USER_ID
WHERE A.STATUS = 'DONE'
GROUP BY B.USER_ID
HAVING SUM(A.PRICE) >= 700000
ORDER BY TOTAL_SALES;
SELECT WRITER_ID, NICKNAME, sum(PRICE) TOTAL_SALES
FROM USED_GOODS_BOARD A join USED_GOODS_USER B on A.WRITER_ID = B.USER_ID
WHERE STATUS = 'DONE'
GROUP BY PRICE
HAVING TOTAL_SALES >= 700000
ORDER BY TOTAL_SALES
→ 이렇게 했는데 틀렸다.. 왜 그럴까?
아차차 바보바보.. GROUP BY 를 사람 기준으로 해야지!!
SELECT WRITER_ID, NICKNAME, sum(PRICE) TOTAL_SALES
FROM USED_GOODS_BOARD A join USED_GOODS_USER B on A.WRITER_ID = B.USER_ID
WHERE STATUS = 'DONE'
GROUP BY WRITER_ID
HAVING TOTAL_SALES >= 700000
ORDER BY TOTAL_SALES