반응형
테이블명과 컬럼명은 회사마다 다릅니다. SQL 도 DBMS 에 따라 조금씩 표현 방법이 다릅니다. 어떤식으로 쿼리문을 작성하면 되는지 이해하며 보시고 상황에 맞게 수정해서 사용해보세요 :)
상품등록 후 최초 30일 판매 데이터
SELECT
it.s2_product_code AS SKU
,it.it_name AS product_name
,it_time AS register_date
--,DATEDIFF(day, it_time, current_date) AS after_reg_date -- 상품등록한지 몇일 지났는지
,SUM(ct.ct_qty) AS first_30days_od_qty
,SUM(ct.ct_price * ct.ct_qty) AS first_30days_od_price
FROM shop_order AS od JOIN shop_cart AS ct
ON od.od_id=ct.od_id
LEFT JOIN shop_item AS it
ON ct.s2_product_code=it.s2_product_code
WHERE it.it_id != ''
AND left(od.od_receipt_time, 10) BETWEEN it_time AND it_time+30 -- 결제 일시 _상품등록 후 최초 30일동안의 결제
AND it_time+30 < current_date --등록한지 30일 지난 sku들만
AND left(it_time,10) BETWEEN '2022-01-01' AND 'current_date' -- 상품등록 일시
AND ct.ct_status IN ('완료','입금','배송','준비')
AND it.s2_product_code NOT LIKE '%freegift%'
GROUP BY it.s2_product_code, it.it_name, it_time
ORDER BY first_30days_od_qty desc
SELECT
it.s2_product_code AS SKU
,it.it_name AS product_name
,it_time AS register_date --상품등록 일시
--,DATEDIFF(day, it_time, current_date) AS after_reg_date
,SUM(ct.ct_qty) AS first_30days_od_qty
,SUM(ct.ct_price * ct.ct_qty) AS first_30days_od_price
FROM shop_order AS od JOIN shop_cart AS ct
ON od.od_id=ct.od_id
LEFT JOIN shop_item AS it
ON ct.s2_product_code=it.s2_product_code
WHERE it.it_id != ''
AND left(od.od_receipt_time, 10) BETWEEN it_time AND it_time+30 -- 결제 일시 _상품등록 후 최초 30일동안의 결제
AND it_time+30 < current_date --등록한지 30일 지난 sku들만
AND left(it_time,10) BETWEEN '2022-01-01' AND 'current_date' -- 상품등록 일시
AND ct.ct_status IN ('완료','입금','배송','준비')
AND it.s2_product_code NOT LIKE '%freegift%'
GROUP BY it.s2_product_code, it.it_name, it_time
ORDER BY first_30days_od_qty desc
코드 설명 :
1. 주문테이블, 카트테이블을 주문번호 컬럼을 key 로 JOIN 한 후 아이템 테이블과 SKU 컬럼을 key 로 또 JOIN
2. current_date 는 오늘 날짜를 반환해주는함수
3. 상품등록한지 30일 지난 sku들만 뽑을 것이고, 상품등록 후 30일 동안의 판매수량, 판매금액을 출력해줄 것임
결과 테이블 형식>
상품등록 후 최초 90일 판매 데이터
SELECT
it.s2_product_code AS SKU
,it.it_name AS product_name
,it_time AS reg_date
,SUM(ct.ct_qty) AS first_90days_od_qty
,SUM(ct.ct_price * ct.ct_qty) AS first_90days_od_price
FROM shop_order AS od JOIN shop_cart AS ct
ON od.od_id=ct.od_id
LEFT JOIN shop_item AS it
ON ct.s2_product_code=it.s2_product_code
WHERE it.it_id != ''
AND left(od.od_receipt_time, 10) BETWEEN it_time AND it_time+90 -- 결제 일시 _상품등록 후 최초 30일동안의 결제
AND left(it_time,10) BETWEEN '2022-01-01' AND 'current_day' -- 상품등록 일시
AND ct.ct_status IN ('완료','입금','배송','준비')
AND it.s2_product_code NOT LIKE '%freegift%'
GROUP BY it.s2_product_code,it.it_name, it_time
ORDER BY first_90days_od_price desc
SELECT
it.s2_product_code AS SKU
,it.it_name AS product_name
,it_time AS reg_date
,SUM(ct.ct_qty) AS first_90days_od_qty
,SUM(ct.ct_price * ct.ct_qty) AS first_90days_od_price
FROM shop_order AS od JOIN shop_cart AS ct
ON od.od_id=ct.od_id
LEFT JOIN shop_item AS it
ON ct.s2_product_code=it.s2_product_code
WHERE it.it_id != ''
AND left(od.od_receipt_time, 10) BETWEEN it_time AND it_time+90 -- 결제 일시 _상품등록 후 최초 30일동안의 결제
AND left(it_time,10) BETWEEN '2022-01-01' AND 'current_day' -- 상품등록 일시
AND ct.ct_status IN ('완료','입금','배송','준비')
AND it.s2_product_code NOT LIKE '%freegift%'
GROUP BY it.s2_product_code,it.it_name, it_time
ORDER BY first_90days_od_price desc
상품등록 후 최초 30일 판매데이터와 거의 같은데 where 조건문에 결제일시 범위만 바꿔주면 됨
left(od.od_receipt_time, 10) BETWEEN it_time AND it_time+90
'IT > SQL' 카테고리의 다른 글
[SQL] 전월 동일 시점 누적 데이터 비교 쿼리문(ft. Line Chart) (0) | 2022.06.13 |
---|---|
[SQL] 월별 방문자수(UV), 월별 가입수 쿼리문 (0) | 2022.05.27 |
[SQL 예문] SQL 연습하기 3 (기초 쿼리문) (0) | 2022.03.24 |
[SQL 예문] SQL 연습하기 2 (기초 쿼리문) (0) | 2022.03.24 |
[SQL 예문] SQL 연습하기 1 (기초 쿼리문) (0) | 2022.03.24 |
댓글