SELECT CUSTOMER_ID, COUNT()
FROM ORDER
GROUP BY CUSTOMOER_ID
HAVING COUNT() >=2 ----- 재구매한 고객 필터링 (WHERE 조건은 불가능, COUNT(*)는 WHERE 조건에서 먼저 실행 되므로 SELECT절 보다 빠르기 때문)
2) 메인 쿼리와 LEFT JOIN 시키기
WITH REORDER AS (
SELECT CUSTOMER_ID, COUNT()
FROM ORDER
GROUP BY CUSTOMOER_ID
HAVING COUNT() >=2
)
SELECT O.*,
CASE WHEN RO.CUSTOMER_ID IS NOT NULL THEN "재구매 고객" END AS "재구매 고객 여부"
FROM ORDER O
LEFT JOIN REORDER RO ON RO.CUSTOMER_ID = O.CUSTOMER_ID