<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.style24.batch.biz.dao.TsbStatisticsDao">
	
	<!-- 페이징을 위한 select절 상단 -->
	<sql id="selectForPagingHeader">
		SELECT *
		FROM (
	</sql>

	<!-- 페이징을 위한 select절 하단 -->
	<sql id="selectForPagingFooter">
		) ORIGINAL
		LIMIT #{limitStartRow} ,  #{pageSize}
	</sql>
	
	<!-- 사이트 제휴코드 유입집계 -->
	<insert id="createInflowDaily" parameterType="String">
		/* TsbStatistics.createInflowDaily */
		INSERT INTO TB_INFLOW_DAILY (
		    INFLOW_YMD
		  , SITE_CD
		  , AF_LINK_CD
		  , PC_INFLOW_TCNT
		  , MOB_INFLOW_TCNT
		  , AOS_INFLOW_TCNT
		  , IOS_INFLOW_TCNT
		  , PC_INFLOW_CNT
		  , MOB_INFLOW_CNT
		  , AOS_INFLOW_CNT
		  , IOS_INFLOW_CNT
		  , REG_NO
		  , REG_DT
		)
		(
		SELECT * FROM (
		       SELECT INFLOW_YMD
		            , SITE_CD
		            , AF_LINK_CD
		            , SUM(CASE WHEN FRONT_GB = 'P' AND APP_YN = 'N' THEN INFLOW_TCNT ELSE 0 END)                   AS PC_INFLOW_TCNT
		            , SUM(CASE WHEN FRONT_GB = 'M' AND APP_YN = 'N' THEN INFLOW_TCNT ELSE 0 END)                   AS MOB_INFLOW_TCNT
		            , SUM(CASE WHEN FRONT_GB = 'M' AND APP_YN = 'Y' AND OS_TYPE = 'A' THEN INFLOW_TCNT ELSE 0 END) AS AOS_INFLOW_TCNT
		            , SUM(CASE WHEN FRONT_GB = 'M' AND APP_YN = 'Y' AND OS_TYPE = 'I' THEN INFLOW_TCNT ELSE 0 END) AS IOS_INFLOW_TCNT
		            , SUM(CASE WHEN FRONT_GB = 'P' AND APP_YN = 'N' THEN INFLOW_CNT ELSE 0 END)                    AS PC_INFLOW_CNT
		            , SUM(CASE WHEN FRONT_GB = 'M' AND APP_YN = 'N' THEN INFLOW_CNT ELSE 0 END)                    AS MOB_INFLOW_CNT
		            , SUM(CASE WHEN FRONT_GB = 'M' AND APP_YN = 'Y' AND OS_TYPE = 'A' THEN INFLOW_CNT ELSE 0 END)  AS AOS_INFLOW_CNT
		            , SUM(CASE WHEN FRONT_GB = 'M' AND APP_YN = 'Y' AND OS_TYPE = 'I' THEN INFLOW_CNT ELSE 0 END)  AS IOS_INFLOW_CNT
		            , 0                                                                                            AS REG_NO
		            , NOW()                                                                                        AS REG_DT
		       FROM   (
		               SELECT DATE_FORMAT(INFLOW_DT,'%Y%m%d') AS INFLOW_YMD
		                    , SITE_CD
		                    , AF_LINK_CD
		                    , FRONT_GB
		                    , APP_YN
		                    , OS_TYPE
		                    , COUNT(DISTINCT JSESSION_ID)   AS INFLOW_TCNT
		                    , COUNT(DISTINCT IP_ADDR)       AS INFLOW_CNT
		               FROM   TB_INFLOW_HST
		               WHERE 1 = 1
		               <if test="inflowDt == 'today'">
		               AND   INFLOW_DT >= DATE_FORMAT(DATE_FORMAT(NOW(), '%Y%m%d'), '%Y%m%d%H%i%S')
		               <![CDATA[
		               AND   INFLOW_DT < DATE_FORMAT(DATE_ADD(DATE_FORMAT(NOW(), '%Y%m%d'), INTERVAL 1 DAY), '%Y%m%d%H%i%S')
		               ]]>
		               </if>
		               <if test="inflowDt == 'yesterday'">
		               AND   INFLOW_DT >= DATE_FORMAT(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d'), '%Y%m%d%H%i%S')
		               <![CDATA[
		               AND   INFLOW_DT < DATE_FORMAT(DATE_ADD(DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d'), INTERVAL 1 DAY), '%Y%m%d%H%i%S')
		               ]]>
		               </if>
		               GROUP  BY DATE_FORMAT(INFLOW_DT,'%Y%m%d'), AF_LINK_CD, SITE_CD, FRONT_GB, APP_YN, OS_TYPE
		              ) Z
		       GROUP  BY INFLOW_YMD, AF_LINK_CD, SITE_CD
		      ) B 
		)
		ON DUPLICATE KEY UPDATE
		   PC_INFLOW_TCNT  = B.PC_INFLOW_TCNT
		 , MOB_INFLOW_TCNT = B.MOB_INFLOW_TCNT
		 , AOS_INFLOW_TCNT = B.AOS_INFLOW_TCNT
		 , IOS_INFLOW_TCNT = B.IOS_INFLOW_TCNT
		 , PC_INFLOW_CNT   = B.PC_INFLOW_CNT
		 , MOB_INFLOW_CNT  = B.MOB_INFLOW_CNT
		 , AOS_INFLOW_CNT  = B.AOS_INFLOW_CNT
		 , IOS_INFLOW_CNT  = B.IOS_INFLOW_CNT
		 , REG_NO          = B.REG_NO
		 , REG_DT          = B.REG_DT
	</insert>

	<!-- 일자별 주문 집계 -->
	<insert id="createOrderDaily" parameterType="String" timeout="600">
	/* TsbStatistics.createOrderDaily */
		INSERT INTO TB_STAT_ORD_DAY (DAY, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN, REAL_ORD_AMT, SELL_QTY, ORD_AMT, GOODS_CPN_AMT, TMTB_AMT, CART_CPN_AMT, CNCL_QTY, RTN_QTY, CNCL_AMT, RTN_AMT, SAVE_PNT_AMT, CHG_QTY, CHG_DELV_FEE, ORD_CUST, USE_PNT_AMT, ORD_CNT, SELF_AMT, CHANNEL_AMT, EXTMALL_AMT, REG_DT)
		WITH TAB AS (
			SELECT *
			FROM (
				SELECT A.ORD_NO
					, B.ORD_DTL_NO
					, B.ORD_QTY
					, B.CNCL_RTN_QTY
					, B.ORD_AMT
					, B.CNCL_RTN_AMT
					, B.ORD_DTL_STAT
					, B.CPN1_DC_AMT
					, B.GOODS_CPN_DC_AMT
					, B.TMTB1_DC_AMT
					, B.TMTB2_DC_AMT
					, B.CART_CPN_DC_AMT
					, B.SAVE_PNT_AMT
					, CASE WHEN B.EXTMALL_ID IS NULL AND B.AF_LINK_CD IS NULL THEN 'AF001' ELSE B.AF_LINK_CD END                                     AS AF_LINK_CD
					, B.EXTMALL_ID
					, A.PAY_DT
					, A.FRONT_GB
					, C.ITEMKIND_CD
					, C.BRAND_CD
					, C.SUPPLY_COMP_CD
					, C.GOODS_CD
					, B.FORMAL_GB
					, CASE WHEN C.DISTRIBUTION_GB = 'G065_20' THEN 'SCM' ELSE 'WMS' END AS DISTRIBUTION_GB
					, IFNULL(FN_DEC_AES(E.SEX_GB), 'G007_X')                            AS SEX_GB
					, CASE WHEN FN_DEC_AES(E.BIRTH_YMD) = '' OR FN_DEC_AES(E.BIRTH_YMD) IS NULL THEN 'X'
					       WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(E.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
						   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(E.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
						   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(E.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
						   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(E.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
						   ELSE 'X' END                                                  AS AGE
					, E.CUST_NO
					, SHOT_DELV_YN
					, B.PNT_DC_AMT
					, B.PRE_PNT_DC_AMT
					, RANK() OVER (PARTITION BY B.ORD_DTL_NO ORDER BY B.ORD_DTL_HST_SQ) AS RNK
				FROM TB_ORDER A,
					TB_ORDER_DETAIL_HST B,
					TB_GOODS C,
					TB_CUSTOMER E
				WHERE A.ORD_NO = B.ORD_NO
					AND B.GOODS_CD = C.GOODS_CD
					AND A.CUST_NO = E.CUST_NO
					AND A.PAY_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')                         -- 시작일변수
					AND A.PAY_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
					AND B.ORD_EXCH_GB = 'O'
					AND B.ORD_DTL_STAT = 'G013_20' -- 결제완료
			) A
			WHERE RNK = 1
		)
		-- 판매수
		, TAB_SELL_QTY AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				, SUM(ORD_QTY) AS VAL
				, '1' AS GBN
				, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM TAB
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 매출
		, TAB_ORD AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				, SUM(ORD_AMT + CPN1_DC_AMT + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = A.ORD_DTL_NO)) AS VAL
				, '2' AS GBN
				, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM TAB A
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 즉시/상품쿠폰
		, TAB_GOODS_CPN AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				, SUM(CPN1_DC_AMT + GOODS_CPN_DC_AMT) AS VAL
				, '3' AS GBN
				, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM TAB A
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 다다익선
		, TAB_TMTB AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				, SUM(TMTB1_DC_AMT + TMTB2_DC_AMT) AS VAL
				, '4' AS GBN
				, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM TAB A
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 장바구니
		, TAB_CART_CPN AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				, SUM(CART_CPN_DC_AMT) AS VAL
				, '5' AS GBN
				, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM TAB A
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 취소갯수
		, TAB_CNCL_QTY AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d') AS TERM
				, SUM(CHG_QTY) AS VAL
				, '6' AS GBN
				, E.GOODS_CD, B.FORMAL_GB, C.FRONT_GB, EXTMALL_ID, (CASE WHEN DISTRIBUTION_GB = 'G065_20' THEN 'SCM' ELSE 'WMS' END) AS DISTRIBUTION_GB, E.SUPPLY_COMP_CD
			    , CASE WHEN B.EXTMALL_ID IS NULL AND B.AF_LINK_CD IS NULL THEN 'AF001' ELSE B.AF_LINK_CD END AS AF_LINK_CD, BRAND_CD, ITEMKIND_CD
				, IFNULL(FN_DEC_AES(G.SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(G.BIRTH_YMD) = '' OR FN_DEC_AES(G.BIRTH_YMD) IS NULL THEN 'X'
				       WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
					   ELSE 'X' END                                                  AS AGE
				, SHOT_DELV_YN
			FROM TB_ORDER_CHANGE_DETAIL A, TB_ORDER_DETAIL B, TB_ORDER C, TB_GOODS E, TB_CUSTOMER G
			WHERE A.ORD_DTL_NO = B.ORD_DTL_NO
				AND B.ORD_NO = C.ORD_NO
				AND B.GOODS_CD = E.GOODS_CD
				AND C.CUST_NO = G.CUST_NO
				AND CHG_STAT = 'G685_18' -- 결제후취소
				AND COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
				AND COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
			GROUP BY TERM, E.GOODS_CD, B.FORMAL_GB, C.FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, E.SUPPLY_COMP_CD, B.AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 반품갯수
		, TAB_RTN_QTY AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d') AS TERM
				, SUM(CHG_QTY) AS VAL
				, '7' AS GBN
				, E.GOODS_CD, B.FORMAL_GB, C.FRONT_GB, EXTMALL_ID, (CASE WHEN DISTRIBUTION_GB = 'G065_20' THEN 'SCM' ELSE 'WMS' END) AS DISTRIBUTION_GB, E.SUPPLY_COMP_CD
			    , CASE WHEN B.EXTMALL_ID IS NULL AND B.AF_LINK_CD IS NULL THEN 'AF001' ELSE B.AF_LINK_CD END AS AF_LINK_CD, BRAND_CD, ITEMKIND_CD
				, IFNULL(FN_DEC_AES(G.SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(G.BIRTH_YMD) = '' OR FN_DEC_AES(G.BIRTH_YMD) IS NULL THEN 'X'
				       WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
					   ELSE 'X' END                                                  AS AGE
				, SHOT_DELV_YN
			FROM TB_ORDER_CHANGE_DETAIL A, TB_ORDER_DETAIL B, TB_ORDER C, TB_GOODS E, TB_CUSTOMER G
			WHERE A.ORD_DTL_NO = B.ORD_DTL_NO
				AND B.ORD_NO = C.ORD_NO
				AND B.GOODS_CD = E.GOODS_CD
				AND C.CUST_NO = G.CUST_NO
				AND CHG_STAT = 'G685_60' -- 반품완료
				AND COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
				AND COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
			GROUP BY TERM, E.GOODS_CD, B.FORMAL_GB, C.FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, E.SUPPLY_COMP_CD, B.AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 취소액
		, TAB_CNCL_AMT AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d') AS TERM
				, SUM((C.CURR_PRICE + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = C.ORD_DTL_NO)) * B.CHG_QTY) AS VAL
				, '8' AS GBN
				, E.GOODS_CD, C.FORMAL_GB, D.FRONT_GB, EXTMALL_ID, (CASE WHEN DISTRIBUTION_GB = 'G065_20' THEN 'SCM' ELSE 'WMS' END) AS DISTRIBUTION_GB, E.SUPPLY_COMP_CD
			    , CASE WHEN C.EXTMALL_ID IS NULL AND C.AF_LINK_CD IS NULL THEN 'AF001' ELSE C.AF_LINK_CD END AS AF_LINK_CD, BRAND_CD, ITEMKIND_CD
				, IFNULL(FN_DEC_AES(G.SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(G.BIRTH_YMD) = '' OR FN_DEC_AES(G.BIRTH_YMD) IS NULL THEN 'X'
				       WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
					   ELSE 'X' END                                                  AS AGE
				, SHOT_DELV_YN
			FROM TB_ORDER_CHANGE_DETAIL B, TB_ORDER_DETAIL C, TB_ORDER D, TB_GOODS E, TB_CUSTOMER G
			WHERE B.ORD_DTL_NO = C.ORD_DTL_NO
				AND C.ORD_NO = D.ORD_NO
				AND C.GOODS_CD = E.GOODS_CD
				AND D.CUST_NO = G.CUST_NO
				AND CHG_STAT = 'G685_18' -- 결제후취소
				AND B.COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
				AND B.COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
			GROUP BY TERM, E.GOODS_CD, C.FORMAL_GB, D.FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, E.SUPPLY_COMP_CD, C.AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 반품액
		, TAB_RTN_AMT AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d') AS TERM
				, SUM((C.CURR_PRICE + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = C.ORD_DTL_NO)) * B.CHG_QTY) AS VAL
				, '9' AS GBN
				, E.GOODS_CD, C.FORMAL_GB, D.FRONT_GB, EXTMALL_ID, (CASE WHEN DISTRIBUTION_GB = 'G065_20' THEN 'SCM' ELSE 'WMS' END) AS DISTRIBUTION_GB, E.SUPPLY_COMP_CD
			    , CASE WHEN C.EXTMALL_ID IS NULL AND C.AF_LINK_CD IS NULL THEN 'AF001' ELSE C.AF_LINK_CD END AS AF_LINK_CD, BRAND_CD, ITEMKIND_CD
				, IFNULL(FN_DEC_AES(G.SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(G.BIRTH_YMD) = '' OR FN_DEC_AES(G.BIRTH_YMD) IS NULL THEN 'X'
				       WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
					   ELSE 'X' END                                                  AS AGE
				, SHOT_DELV_YN
			FROM TB_ORDER_CHANGE_DETAIL B, TB_ORDER_DETAIL C, TB_ORDER D, TB_GOODS E, TB_CUSTOMER G
			WHERE B.ORD_DTL_NO = C.ORD_DTL_NO
				AND C.ORD_NO = D.ORD_NO
				AND C.GOODS_CD = E.GOODS_CD
				AND D.CUST_NO = G.CUST_NO
				AND CHG_STAT = 'G685_60' -- 반품완료
				AND B.COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
				AND B.COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
			GROUP BY TERM, E.GOODS_CD, C.FORMAL_GB, D.FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, E.SUPPLY_COMP_CD, C.AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 지급포인트
		, TAB_SAVE_PNT AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				, SUM(SAVE_PNT_AMT) AS VAL
				, '10' AS GBN
				, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM TAB A
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 교환갯수
		, TAB_CHG_QTY AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d') AS TERM
				, SUM(CHG_QTY) AS VAL
				, '11' AS GBN
				, E.GOODS_CD, B.FORMAL_GB, C.FRONT_GB, EXTMALL_ID, (CASE WHEN DISTRIBUTION_GB = 'G065_20' THEN 'SCM' ELSE 'WMS' END) AS DISTRIBUTION_GB, E.SUPPLY_COMP_CD
			    , CASE WHEN B.EXTMALL_ID IS NULL AND B.AF_LINK_CD IS NULL THEN 'AF001' ELSE B.AF_LINK_CD END AS AF_LINK_CD, BRAND_CD, ITEMKIND_CD
				, IFNULL(FN_DEC_AES(G.SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(G.BIRTH_YMD) = '' OR FN_DEC_AES(G.BIRTH_YMD) IS NULL THEN 'X'
				       WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
					   ELSE 'X' END                                                  AS AGE
				, SHOT_DELV_YN
			FROM TB_ORDER_CHANGE_DETAIL A, TB_ORDER_DETAIL B, TB_ORDER C, TB_GOODS E, TB_CUSTOMER G
			WHERE A.ORD_DTL_NO = B.ORD_DTL_NO
				AND B.ORD_NO = C.ORD_NO
				AND B.GOODS_CD = E.GOODS_CD
				AND C.CUST_NO = G.CUST_NO
				AND CHG_STAT = 'G685_40' -- 교환완료
				AND COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
				AND COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
			GROUP BY TERM, E.GOODS_CD, B.FORMAL_GB, C.FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, E.SUPPLY_COMP_CD, B.AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 교환배송비
		, TAB_CHG_DELV_FEE AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d') AS TERM
				, SUM(A.DELV_FEE) AS VAL
				, '12' AS GBN
				, E.GOODS_CD, B.FORMAL_GB, C.FRONT_GB, EXTMALL_ID, (CASE WHEN DISTRIBUTION_GB = 'G065_20' THEN 'SCM' ELSE 'WMS' END) AS DISTRIBUTION_GB, E.SUPPLY_COMP_CD
			    , CASE WHEN B.EXTMALL_ID IS NULL AND B.AF_LINK_CD IS NULL THEN 'AF001' ELSE B.AF_LINK_CD END AS AF_LINK_CD, BRAND_CD, ITEMKIND_CD
				, IFNULL(FN_DEC_AES(G.SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(G.BIRTH_YMD) = '' OR FN_DEC_AES(G.BIRTH_YMD) IS NULL THEN 'X'
				       WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
					   WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
					   ELSE 'X' END                                                  AS AGE
				, SHOT_DELV_YN
			FROM (
				SELECT A.ORD_CHG_SQ
					, A.ORD_DTL_NO
					, CASE WHEN A.RNK = 1 THEN DELV_FEE ELSE 0 END AS DELV_FEE
				FROM (
					SELECT OC.ORD_CHG_SQ
					, OC.ORD_DTL_NO
					, DF.DELV_FEE
					, RANK() OVER (PARTITION BY OC.ORD_CHG_SQ, DF.DELV_FEE_CD ORDER BY OC.ORD_DTL_NO) AS RNK
					FROM TB_ORDER_CHANGE_DETAIL OC
					LEFT OUTER JOIN TB_DELIVERY_FEE DF
					ON DF.ORD_CHG_SQ = OC.ORD_CHG_SQ AND DF.DELV_FEE_GB = 'G018_30'
					WHERE OC.CHG_STAT = 'G685_40' -- 교환완료
					AND OC.COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
					AND OC.COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
				) A
			) A, TB_ORDER_CHANGE_DETAIL A1, TB_ORDER_DETAIL B, TB_ORDER C, TB_GOODS E, TB_CUSTOMER G
			WHERE A.ORD_CHG_SQ = A1.ORD_CHG_SQ
				AND A.ORD_DTL_NO = A1.ORD_DTL_NO
				AND A1.ORD_DTL_NO = B.ORD_DTL_NO
				AND B.ORD_NO = C.ORD_NO
				AND B.GOODS_CD = E.GOODS_CD
				AND C.CUST_NO = G.CUST_NO
				AND A1.CHG_STAT = 'G685_40' -- 교환완료
				AND A1.COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
				AND A1.COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
			GROUP BY TERM, E.GOODS_CD, B.FORMAL_GB, C.FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, E.SUPPLY_COMP_CD, B.AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 구매자수(비회원은 주문당 1명)
		, TAB_ORD_CUST AS (
			SELECT
				TERM
				, SUM(VAL) AS VAL
				, '13' AS GBN
				, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM (
				SELECT
					DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
					, 1 AS VAL
					, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
				FROM TAB
				WHERE CUST_NO > 0
				GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
				UNION ALL
				SELECT
					DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
					, 1 AS VAL
					, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
				FROM TAB
				WHERE CUST_NO = 0 -- 비회원
				GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			) A
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		-- 사용포인트
		, TAB_USE_PNT AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				, SUM(PNT_DC_AMT + PRE_PNT_DC_AMT) AS VAL
				, '14' AS GBN
				, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM TAB A
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
	   -- 주문수
	   , TAB_ORD_CNT AS (
			SELECT
				TERM
				 , SUM(VAL) AS VAL
				 , '15' AS GBN
				 , GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM (
					 SELECT
						 DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
						  , 1 AS VAL
						  , ORD_NO
						  , GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
					 FROM TAB
					 GROUP BY TERM, ORD_NO, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
				 ) A
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
	   -- 자사직방문
	   , TAB_SELF AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				 , SUM(ORD_AMT + CPN1_DC_AMT + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = A.ORD_DTL_NO)) AS VAL
				 , '16' AS GBN
				 , GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM TAB A
			WHERE EXISTS(
					  SELECT 1
					  FROM TB_AF_LINK X
					  WHERE X.AF_LINK_CD = A.AF_LINK_CD
						AND X.AF_CHANNEL = 'G053_01'
				  )
				AND EXTMALL_ID IS NULL
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
	   -- 채널
	   , TAB_CHANNEL AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				 , SUM(ORD_AMT + CPN1_DC_AMT + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = A.ORD_DTL_NO)) AS VAL
				 , '17' AS GBN
				 , GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM TAB A
			WHERE EXISTS(
					  SELECT 1
					  FROM TB_AF_LINK X
					  WHERE X.AF_LINK_CD = A.AF_LINK_CD
						AND X.AF_CHANNEL != 'G053_01'
				  )
				AND EXTMALL_ID IS NULL
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
	   -- 제휴몰
	   , TAB_EXTMALL AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				 , SUM(ORD_AMT + CPN1_DC_AMT + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = A.ORD_DTL_NO)) AS VAL
				 , '18' AS GBN
				 , GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			FROM TAB A
			WHERE EXTMALL_ID IS NOT NULL
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		)
		SELECT
			TERM
			, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
			, ORD_AMT - (GOODS_CPN_AMT + TMTB_AMT + CART_CPN_AMT + USE_PNT_AMT) AS REAL_ORD_AMT
			, SELL_QTY, ORD_AMT, GOODS_CPN_AMT, TMTB_AMT, CART_CPN_AMT, CNCL_QTY, RTN_QTY, CNCL_AMT, RTN_AMT, SAVE_PNT_AMT
			, CHG_QTY, CHG_DELV_FEE, ORD_CUST, USE_PNT_AMT, ORD_CNT
			, SELF_AMT, CHANNEL_AMT, EXTMALL_AMT
			, NOW()
		FROM (
			SELECT
				TERM
				, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
				, SUM(CASE WHEN GBN = '1' THEN VAL ELSE 0 END) AS SELL_QTY -- 판매수
				, SUM(CASE WHEN GBN = '2' THEN VAL ELSE 0 END) AS ORD_AMT -- 매출액(판매금액)
				, SUM(CASE WHEN GBN = '3' THEN VAL ELSE 0 END) AS GOODS_CPN_AMT -- 즉시+상품쿠폰금액
				, SUM(CASE WHEN GBN = '4' THEN VAL ELSE 0 END) AS TMTB_AMT -- 다다익선금액
				, SUM(CASE WHEN GBN = '5' THEN VAL ELSE 0 END) AS CART_CPN_AMT -- 장바구니쿠폰금액
				, SUM(CASE WHEN GBN = '6' THEN VAL ELSE 0 END) AS CNCL_QTY -- 취소수량
				, SUM(CASE WHEN GBN = '7' THEN VAL ELSE 0 END) AS RTN_QTY -- 반품수량
				, SUM(CASE WHEN GBN = '8' THEN VAL ELSE 0 END) AS CNCL_AMT -- 취소금액
				, SUM(CASE WHEN GBN = '9' THEN VAL ELSE 0 END) AS RTN_AMT -- 반품금액
				, SUM(CASE WHEN GBN = '10' THEN VAL ELSE 0 END) AS SAVE_PNT_AMT -- 지금포인트
				, SUM(CASE WHEN GBN = '11' THEN VAL ELSE 0 END) AS CHG_QTY -- 교환수량
				, SUM(CASE WHEN GBN = '12' THEN VAL ELSE 0 END) AS CHG_DELV_FEE -- 교환배송비
				, SUM(CASE WHEN GBN = '13' THEN VAL ELSE 0 END) AS ORD_CUST -- 구매자수
				, SUM(CASE WHEN GBN = '14' THEN VAL ELSE 0 END) AS USE_PNT_AMT -- 사용포인트
				, SUM(CASE WHEN GBN = '15' THEN VAL ELSE 0 END) AS ORD_CNT -- 주문수
				, SUM(CASE WHEN GBN = '16' THEN VAL ELSE 0 END) AS SELF_AMT -- 자사몰매출
				, SUM(CASE WHEN GBN = '17' THEN VAL ELSE 0 END) AS CHANNEL_AMT -- 채널매출
				, SUM(CASE WHEN GBN = '18' THEN VAL ELSE 0 END) AS EXTMALL_AMT -- 제휴몰매출
			FROM (
				SELECT * FROM TAB_SELL_QTY
				UNION ALL
				SELECT * FROM TAB_ORD
				UNION ALL
				SELECT * FROM TAB_GOODS_CPN
				UNION ALL
				SELECT * FROM TAB_TMTB
				UNION ALL
				SELECT * FROM TAB_CART_CPN
				UNION ALL
				SELECT * FROM TAB_CNCL_QTY
				UNION ALL
				SELECT * FROM TAB_RTN_QTY
				UNION ALL
				SELECT * FROM TAB_CNCL_AMT
				UNION ALL
				SELECT * FROM TAB_RTN_AMT
				UNION ALL
				SELECT * FROM TAB_SAVE_PNT
				UNION ALL
				SELECT * FROM TAB_CHG_QTY
				UNION ALL
				SELECT * FROM TAB_CHG_DELV_FEE
				UNION ALL
				SELECT * FROM TAB_ORD_CUST
				UNION ALL
				SELECT * FROM TAB_USE_PNT
				UNION ALL
				SELECT * FROM TAB_ORD_CNT
				UNION ALL
				SELECT * FROM TAB_SELF
				UNION ALL
				SELECT * FROM TAB_CHANNEL
				UNION ALL
				SELECT * FROM TAB_EXTMALL
			) A
			GROUP BY TERM, GOODS_CD, FORMAL_GB, FRONT_GB, EXTMALL_ID, DISTRIBUTION_GB, SUPPLY_COMP_CD, AF_LINK_CD, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, SHOT_DELV_YN
		) X
	</insert>

	<!-- 시간대별 주문 집계 -->
	<insert id="createOrderDateTime" parameterType="String" timeout="600">
	/* TsbStatistics.createOrderDateTime */
		INSERT INTO TB_STAT_ORD_TIME (DAY_TIME, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN, ORD_AMT, CNCL_AMT, ORD_CNT, SELL_QTY, CNCL_QTY, ORD_CUST, CUST_PRICE, REG_DT)
		WITH TAB AS (
			SELECT *
			FROM (
				SELECT
					A.ORD_NO
					, B.ORD_DTL_NO
					, B.ORD_QTY
					, B.CNCL_RTN_QTY
					, B.ORD_AMT
					, B.CNCL_RTN_AMT
					, B.ORD_DTL_STAT
					, B.EXTMALL_ID
					, A.PAY_DT
					, A.FRONT_GB
					, C.ITEMKIND_CD
					, C.BRAND_CD
					, C.GOODS_CD
					, A.CUST_NO
					, B.SHOT_DELV_YN
					, CPN1_DC_AMT
					, RANK() OVER (PARTITION BY B.ORD_DTL_NO ORDER BY B.ORD_DTL_HST_SQ) AS RNK
				FROM TB_ORDER A, TB_ORDER_DETAIL_HST B, TB_GOODS C, TB_CUSTOMER D
				WHERE A.ORD_NO = B.ORD_NO
				    AND B.GOODS_CD = C.GOODS_CD
				    AND A.CUST_NO = D.CUST_NO
					AND A.PAY_DT >= DATE_ADD(DATE_FORMAT(CONCAT(#{currDay}, '0000'), '%Y%m%d%H%i%s'), INTERVAL -1 HOUR )                 -- 시작일변수
					AND A.PAY_DT &lt; DATE_FORMAT(CONCAT(#{currDay}, '0000'), '%Y%m%d%H%i%s') -- 종료일변수
					AND B.ORD_EXCH_GB = 'O'
					AND B.ORD_DTL_STAT = 'G013_20' -- 결제완료
			) A
			WHERE RNK = 1
		)
		-- 매출
		, TAB_ORD AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d %H') AS TERM
				, SUM(ORD_AMT + CPN1_DC_AMT + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = A.ORD_DTL_NO)) AS VAL
				, '1' AS GBN
				, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			FROM TAB A
			GROUP BY TERM, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
		)
		-- 취소반품액
		, TAB_CNCL AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d %H') AS TERM
				, SUM((C.CURR_PRICE + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = C.ORD_DTL_NO)) * B.CHG_QTY) AS VAL
				, '2' AS GBN
				, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			FROM TB_ORDER_CHANGE_DETAIL B, TB_ORDER_DETAIL C, TB_ORDER D, TB_GOODS E
			WHERE B.ORD_DTL_NO = C.ORD_DTL_NO
				AND C.ORD_NO = D.ORD_NO
				AND C.GOODS_CD = E.GOODS_CD
				AND CHG_STAT IN ('G685_18','G685_60') -- 결제후취소,반품완료
			    AND B.COMPLETE_DT >= DATE_ADD(DATE_FORMAT(CONCAT(#{currDay}, '0000'), '%Y%m%d%H%i%s'), INTERVAL -1 HOUR ) -- 시작일변수
			    AND B.COMPLETE_DT &lt; DATE_FORMAT(CONCAT(#{currDay}, '0000'), '%Y%m%d%H%i%s') -- 종료일변수
			GROUP BY TERM, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
		)
		-- 주문수
		, TAB_ORD_CNT AS (
			SELECT
				TERM
				, SUM(VAL) AS VAL
				, '3' AS GBN
				, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			FROM (
				SELECT
					DATE_FORMAT(PAY_DT, '%Y-%m-%d %H') AS TERM
					, 1 AS VAL
					, ORD_NO
					, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
				FROM TAB
				GROUP BY TERM, ORD_NO, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			) A
			GROUP BY TERM, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
		)
		-- 판매수
		, TAB_SELL_QTY AS (
			SELECT
				TERM
				, SUM(VAL) AS VAL
				, '4' AS GBN
				, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			FROM (
				SELECT
					DATE_FORMAT(PAY_DT, '%Y-%m-%d %H') AS TERM
					, SUM(ORD_QTY) AS VAL
					, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
				FROM TAB
				GROUP BY TERM, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
				UNION ALL
				SELECT
					DATE_FORMAT(A.REG_DT, '%Y-%m-%d %H') AS TERM
					, SUM(CHG_QTY) * -1 AS VAL
					, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
				FROM TB_ORDER_CHANGE_DETAIL A, TB_ORDER_DETAIL B, TB_ORDER C, TB_GOODS E
				WHERE A.ORD_DTL_NO = B.ORD_DTL_NO
					AND B.ORD_NO = C.ORD_NO
					AND B.GOODS_CD = E.GOODS_CD
					AND CHG_STAT = 'G685_10' -- 취소접수
				    AND A.REG_DT >= DATE_ADD(DATE_FORMAT(CONCAT(#{currDay}, '0000'), '%Y%m%d%H%i%s'), INTERVAL -1 HOUR ) -- 시작일변수
				    AND A.REG_DT &lt; DATE_FORMAT(CONCAT(#{currDay}, '0000'), '%Y%m%d%H%i%s') -- 종료일변수
				GROUP BY TERM, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			) A
			GROUP BY TERM, FRONT_GB, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
		)
		-- 취소반품갯수
		, TAB_CNCL_QTY AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d %H') AS TERM
				, SUM(CHG_QTY) AS VAL
				, '5' AS GBN
				, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			FROM TB_ORDER_CHANGE_DETAIL A, TB_ORDER_DETAIL B, TB_ORDER C, TB_GOODS E
			WHERE A.ORD_DTL_NO = B.ORD_DTL_NO
				AND B.ORD_NO = C.ORD_NO
				AND B.GOODS_CD = E.GOODS_CD
				AND CHG_STAT IN ('G685_18','G685_60') -- 결제후취소,반품완료
			    AND COMPLETE_DT >= DATE_ADD(DATE_FORMAT(CONCAT(#{currDay}, '0000'), '%Y%m%d%H%i%s'), INTERVAL -1 HOUR ) -- 시작일변수
			    AND COMPLETE_DT &lt; DATE_FORMAT(CONCAT(#{currDay}, '0000'), '%Y%m%d%H%i%s') -- 종료일변수
			GROUP BY TERM, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
		)
		-- 주문자수(비회원은 주문당 1명)
		, TAB_ORD_CUST AS (
			SELECT
				TERM
				, SUM(VAL) AS VAL
				, '6' AS GBN
				, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			FROM (
				SELECT
					DATE_FORMAT(PAY_DT, '%Y-%m-%d %H') AS TERM
					, 1 AS VAL
					, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
				FROM TAB
				WHERE CUST_NO > 0
				GROUP BY TERM, CUST_NO, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
				UNION ALL
				SELECT
					DATE_FORMAT(PAY_DT, '%Y-%m-%d %H') AS TERM
					, 1 AS VAL
					, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
				FROM TAB
				WHERE CUST_NO = 0 -- 비회원
				GROUP BY TERM, ORD_NO, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			) A
			GROUP BY TERM, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
		)
		-- 객단가
		, TAB_CUST_PRICE AS (
			SELECT
				TERM
				, FLOOR(SUM(VAL) / SUM(ORD_CNT)) AS VAL
				, '7' AS GBN
				, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			FROM (
				SELECT
					DATE_FORMAT(PAY_DT, '%Y-%m-%d %H') AS TERM
					, SUM(ORD_AMT + CPN1_DC_AMT + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = A.ORD_DTL_NO)) AS VAL
					, 1 AS ORD_CNT
					, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
				FROM TAB A
				GROUP BY TERM, ORD_NO, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			) A
			GROUP BY TERM, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
		)
		SELECT
			TERM
			, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
			, ORD_AMT
			, CNCL_AMT, ORD_CNT, SELL_QTY, CNCL_QTY
			, ORD_CUST, CUST_PRICE
			, NOW()
		FROM (
			SELECT
				TERM
				, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
				, SUM(CASE WHEN GBN = '1' THEN VAL ELSE 0 END) AS ORD_AMT
				, SUM(CASE WHEN GBN = '2' THEN VAL ELSE 0 END) AS CNCL_AMT
				, SUM(CASE WHEN GBN = '3' THEN VAL ELSE 0 END) AS ORD_CNT
				, SUM(CASE WHEN GBN = '4' THEN VAL ELSE 0 END) AS SELL_QTY
				, SUM(CASE WHEN GBN = '5' THEN VAL ELSE 0 END) AS CNCL_QTY
				, SUM(CASE WHEN GBN = '6' THEN VAL ELSE 0 END) AS ORD_CUST
				, SUM(CASE WHEN GBN = '7' THEN VAL ELSE 0 END) AS CUST_PRICE
			FROM (
				SELECT * FROM TAB_ORD
				UNION ALL
				SELECT * FROM TAB_CNCL
				UNION ALL
				SELECT * FROM TAB_ORD_CNT
				UNION ALL
				SELECT * FROM TAB_SELL_QTY
				UNION ALL
				SELECT * FROM TAB_CNCL_QTY
				UNION ALL
				SELECT * FROM TAB_ORD_CUST
				UNION ALL
				SELECT * FROM TAB_CUST_PRICE
			) A
			GROUP BY TERM, FRONT_GB, EXTMALL_ID, BRAND_CD, ITEMKIND_CD, SHOT_DELV_YN
		) X
	</insert>

	<!-- 결제수단통계 -->
	<insert id="createStatPay" parameterType="String" timeout="600">
		/* TsbStatistics.createStatPay */
		INSERT INTO TB_STAT_PAY (DAY, PAY_MEANS, PG_GB, COM_CARD_CD, PAY_AMT, REG_DT)
		SELECT
			DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
			, PAY_MEANS -- 결제수단
			, PG_GB -- PG구분
			, COM_CARD_CD -- 카드코드
			, SUM(PAY_AMT) AS PAY_AMT -- 결제금액
			, NOW()
		FROM TB_PAYMENT A USE INDEX (IX_PAYMENT_01)
		WHERE PAY_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')
			AND PAY_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s')
			AND PAY_STAT IN ('G016_30', 'G016_99')
			AND PAY_MEANS IN ('G014_10','G014_20','G014_30','G014_60','G014_50')
		GROUP BY TERM, PAY_MEANS, PG_GB, COM_CARD_CD
	</insert>

	<!-- 부결제수단통계 -->
	<insert id="createStatSubPay" parameterType="String" timeout="600">
		/* TsbStatistics.createStatSubPay */
		INSERT INTO TB_STAT_SUB_PAY (DAY, OCCUR_GB, PNT_GIVE, PNT_ORD_CNT, PNT_USE, GIFT_REG_CNT, GIFT_ORD_CNT, GIFT_USE, REG_DT)
		WITH TAB_PNT AS (
			SELECT
				DATE_FORMAT(PNT_UPLOAD_DT, '%Y-%m-%d') AS TERM
				, ORD_NO
				, PNT_AMT
				, OCCUR_GB
				, CUST_PNT_SQ
			FROM TB_CUST_POINT_HST
			WHERE PNT_UPLOAD_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')
			AND PNT_UPLOAD_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s')
			AND PNT_UPLOAD_STAT = 'G070_30'
		)
		-- 포인트 적립액
		, PNT_GIVE AS (
			SELECT
				TERM
				, SUM(PNT_AMT) AS VAL
				, OCCUR_GB
				, '1' AS GB
			FROM TAB_PNT
			WHERE 1=1
			GROUP BY TERM, OCCUR_GB
		)
		-- 포인트사용주문수
		, PNT_ORD_CNT AS (
			SELECT
				TERM
				, SUM(CNT) AS VAL
				, OCCUR_GB
				, '2' AS GB
			FROM (
				SELECT
					B.TERM
					, 1 AS CNT
					, OCCUR_GB
				FROM TB_ORDER_DETAIL A, TAB_PNT B
				WHERE A.ORD_NO = B.ORD_NO
					AND A.ORD_QTY > A.CNCL_RTN_QTY
				GROUP BY B.TERM, A.ORD_NO, OCCUR_GB
			) A
			GROUP BY TERM, OCCUR_GB
		)
		-- 포인트사용액
		, PNT_USE AS (
			SELECT
				B.TERM
				, SUM(A.PNT_AMT * -1) AS VAL
				, B.OCCUR_GB
				, '3' AS GB
			FROM TB_CUST_POINT_HST A, TAB_PNT B
			WHERE A.ORD_NO = B.ORD_NO
				AND A.CUST_PNT_SQ = B.CUST_PNT_SQ
				AND A.OCCUR_GB IN ('G069_10', 'G069_11', 'G069_36', 'G069_40', 'G069_41', 'G069_42', 'G069_43')
			GROUP BY TERM, B.OCCUR_GB
		)
		, TAB_GIFT AS (
			SELECT
				DATE_FORMAT(REG_DT, '%Y-%m-%d') AS TERM
				, ORD_NO
				, GFCD_AMT
				, OCCUR_GB
				, CUST_GFCD_SQ
			FROM TB_CUST_GIFTCARD_HST
			WHERE REG_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')
			AND REG_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s')
		)
		-- 상품권등록매수
		, GIFT_REG_QTY AS (
			SELECT
				TERM
				, COUNT(1) AS CNT
				, OCCUR_GB
				, '4' AS GB
			FROM TAB_GIFT
			WHERE OCCUR_GB = 'G074_11'
			GROUP BY TERM, OCCUR_GB
		)
		-- 상품권사용주문수
		, GIFT_ORD_CNT AS (
			SELECT
				TERM
				, SUM(CNT) AS VAL
				, OCCUR_GB
				, '5' AS GB
			FROM (
				SELECT
					B.TERM
					, 1 AS CNT
					, OCCUR_GB
				FROM TB_ORDER_DETAIL A, TAB_GIFT B
				WHERE A.ORD_NO = B.ORD_NO
					AND A.ORD_QTY > A.CNCL_RTN_QTY
				GROUP BY B.TERM, A.ORD_NO, OCCUR_GB
			) A
			GROUP BY TERM, OCCUR_GB
		)
		-- 상품권사용액
		, GIFT_USE AS (
			SELECT
				B.TERM
				, SUM(A.GFCD_AMT * -1) AS VAL
				, B.OCCUR_GB
				, '6' AS GB
			FROM TB_CUST_GIFTCARD_HST A, TAB_GIFT B
			WHERE A.ORD_NO = B.ORD_NO
				AND A.CUST_GFCD_SQ = B.CUST_GFCD_SQ
				AND A.OCCUR_GB IN ('G074_12', 'G074_13', 'G074_18', 'G074_19')
			GROUP BY TERM, B.OCCUR_GB
		)
		SELECT
			DATE_FORMAT(TERM, '%Y-%m-%d') AS TERM
			, OCCUR_GB
			, SUM(CASE WHEN GB = '1' THEN VAL ELSE 0 END) AS PNT_GIVE
			, SUM(CASE WHEN GB = '2' THEN VAL ELSE 0 END) AS PNT_ORD_CNT
			, SUM(CASE WHEN GB = '3' THEN VAL ELSE 0 END) AS PNT_USE
			, SUM(CASE WHEN GB = '4' THEN VAL ELSE 0 END) AS GIFT_REG_CNT
			, SUM(CASE WHEN GB = '5' THEN VAL ELSE 0 END) AS GIFT_ORD_CNT
			, SUM(CASE WHEN GB = '6' THEN VAL ELSE 0 END) AS GIFT_USE
			, NOW()
		FROM (
			SELECT *
			FROM PNT_GIVE
			UNION ALL
			SELECT *
			FROM PNT_USE
			UNION
			SELECT *
			FROM PNT_ORD_CNT
			UNION
			SELECT *
			FROM GIFT_REG_QTY
			UNION
			SELECT *
			FROM GIFT_ORD_CNT
			UNION
			SELECT *
			FROM GIFT_USE
		) Z
		GROUP BY TERM, OCCUR_GB
	</insert>

	<!-- 당일 매출문자 조회 -->
	<select id="getCurrentDaySalesSms" resultType="SalesSms">
		/* TsbStatistics.getCurrentDaySalesSms */
		SELECT SUM(ODIH.SELL_AMT)       AS TOT_NET_SALES_AMT  /*총순매출매금액(즉시할인쿠폰과 상품쿠폰만 차감. 2021.8.31일 할인전금액으로 적용하기로 박중재팀장이 요청에 따라 제거)*/
		     , - 1 * SUM(ODIH.CNCL_AMT) AS TOT_CNCL_AMT       /*총취소금액(즉시할인쿠폰과 상품쿠폰만 차감. 2021.8.31일 할인전금액으로 적용하기로 박중재팀장이 요청에 따라 제거)*/
		     , SUM(CASE WHEN ODIH.SUPPLY_COMP_CD IN (5,4) /*한세MK, FRJ*/ THEN
		                    ODIH.SELL_AMT
		                ELSE
		                    0
		           END)                 AS MK_NET_SALES_AMT   /*한세MK순매출금액*/
		     , SUM(CASE WHEN ODIH.SUPPLY_COMP_CD = 3 THEN /*한세드림*/
		                    ODIH.SELL_AMT
		                ELSE
		                    0
		           END)                 AS DR_NET_SALES_AMT   /*한세드림순매출금액*/
		FROM   (
		        SELECT OD.SUPPLY_COMP_CD
		             , (CASE WHEN ODIH.ORD_DTL_STAT = 'G720_10' THEN 1 ELSE -1 END)
		               *
		               CAST(CASE WHEN ODIH.ORD_DTL_STAT = 'G720_10'/*판매-결제완료*/ THEN
		                             ((ODIH.ORD_QTY - ODIH.CNCL_RTN_QTY) * (CASE WHEN O.MALL_GB = 'G011_20' /*제휴몰*/ THEN
		                                                                             IFNULL(EUP.USAC_PRICE,ODIH.ITEM_PRICE)
		                                                                         ELSE
		                                                                             ODIH.ITEM_PRICE
		                                                                    END))
		                         ELSE
		                             0
		                    END AS SIGNED INT)                                       AS SELL_AMT          /*판매금액*/
		             , CAST(CASE WHEN ODIH.ORD_DTL_STAT = 'G720_10'/*판매-결제완료*/ THEN
		                             0
		                         ELSE
		                             (ODIH.CNCL_RTN_QTY * (CASE WHEN O.MALL_GB = 'G011_20' /*제휴몰*/ THEN
		                                                            IFNULL(EUP.USAC_PRICE,ODIH.ITEM_PRICE)
		                                                        ELSE
		                                                            ODIH.ITEM_PRICE
		                                                   END))
		                    END AS SIGNED INT)                                       AS CNCL_AMT          /*취소금액*/
		             , ODIH.CPN1_DC_AMT                                                                   /*즉시사용쿠폰금액*/
		             , ODIH.GOODS_CPN_DC_AMT                                                              /*상품쿠폰사용금액*/
		             , ODIH.CART_CPN_DC_AMT                                                               /*주문서쿠폰사용금액*/
		             , ODIH.PNT_DC_AMT                                                                    /*포인트사용금액*/
		             , ODIH.TMTB1_DC_AMT + ODIH.TMTB2_DC_AMT AS TMTB_DC_AMT                               /*다다익선할인금액*/
		             , ODIH.GFCD_USE_AMT                                                                  /*상품권사용금액*/
		        FROM   TB_ORDER_DETAIL_ITEM_HST ODIH
		        INNER JOIN TB_ORDER_DETAIL OD ON ODIH.ORD_DTL_NO = OD.ORD_DTL_NO
		                                     AND OD.SUPPLY_COMP_CD IN (3,5,4) /*한세드림,한세MK,FRJ*/
		        INNER JOIN TB_ORDER O ON ODIH.ORD_NO = O.ORD_NO
		        INNER JOIN TB_SUPPLY_COMPANY SC ON OD.SUPPLY_COMP_CD = SC.SUPPLY_COMP_CD
		        LEFT OUTER JOIN TB_EXTMALL_USAC_PRICE EUP ON ODIH.ORD_DTL_ITEM_HST_SQ = EUP.ORD_DTL_ITEM_HST_SQ
		        WHERE  1 = 1
		        AND    ODIH.REG_DT <![CDATA[>=]]> CURRENT_DATE()
		        AND    ODIH.REG_DT <![CDATA[<]]> DATE_ADD(CURRENT_DATE(), INTERVAL 1 DAY)
		        AND    ODIH.ORD_DTL_STAT IN ('G720_10','G720_30','G720_40','G720_50','G720_60') /*판매-결제완료, 환입-취소완료, 환입-품절취소, 환입-반품완료, 환입-교환완료*/
		       ) ODIH
	</select>
	
	<!-- 어제일자 매출반영목록 생성 -->
	<insert id="createYesterdaySalesUploadList" parameterType="String">
		/* TsbStatistics.createYesterdaySalesUploadList */
		INSERT INTO TB_HANSAE_SALES (
		       ERP_GB
		     , DT_SALE
		     , TP_SALE
		     , CD_STYLE
		     , CD_COLOR
		     , CD_SIZE
		     , BRAND_CD
		     , QT_SALE
		     , AM_ACSALE
		     , DS_REMARK
		     , REG_DT
		)
		SELECT #{erpGb}                              AS ERP_GB
		     , ODIH.DT_SALE
		     , ODIH.TP_SALE
		     , IFNULL(HS.ERP_CD_STYLE,ODIH.CD_STYLE) AS CD_STYLE
		     , IFNULL(HS.ERP_CD_COLOR,ODIH.CD_COLOR) AS CD_COLOR
		     , IFNULL(HS.ERP_CD_SIZE,ODIH.CD_SIZE)   AS CD_SIZE
		     , ODIH.BRAND_CD
		     , SUM(ODIH.QT_SALE)                     AS QT_SALE
		     , SUM(ODIH.AM_ACSALE)                   AS AM_ACSALE
		     , ''                                    AS DS_REMARK
		     , NOW()                                 AS REG_DT
		FROM   (
		        SELECT DATE_FORMAT(ODIH.REG_DT,'%Y%m%d')       AS DT_SALE  /*판매일자(=발생일자)*/
		             , CASE WHEN ODIH.ORD_DTL_STAT = 'G720_20' THEN '1'
		                    ELSE '2'
		               END                                     AS TP_SALE  /*판매구분(1:판매, 2:환불)*/
		             , SUBSTRING(ODIH.SKU_MODEL_NO,1,10)       AS CD_STYLE /*ERP스타일코드*/
		             , SUBSTRING(ODIH.SKU_MODEL_NO,11,2)       AS CD_COLOR /*ERP색상코드*/
		             , TRIM(SUBSTRING(ODIH.SKU_MODEL_NO,13,3)) AS CD_SIZE  /*ERP사이즈코드*/
		             , G.BRAND_CD                                          /*브랜드코드*/
		             , SUM(CAST(CASE WHEN ODIH.ORD_DTL_STAT = 'G720_20' THEN ODIH.ORD_QTY - ODIH.CNCL_RTN_QTY /*판매-배송중*/
		                             ELSE ODIH.CNCL_RTN_QTY /*환입-반품완료,환입-교환완료*/
		                        END AS SIGNED INT))            AS QT_SALE  /*판매수량*/
		             , SUM(CAST(CASE WHEN ODIH.ORD_DTL_STAT = 'G720_20' /*판매-배송중*/ THEN
		                                 ((ODIH.ORD_QTY - ODIH.CNCL_RTN_QTY) * (CASE WHEN O.MALL_GB = 'G011_20' /*제휴몰*/ THEN
		                                                                                 IFNULL(EUP.USAC_PRICE,ODIH.ITEM_PRICE)
		                                                                             ELSE
		                                                                                 ODIH.ITEM_PRICE
		                                                                        END))
		                             ELSE /*환입-반품완료,환입-교환완료*/
		                                 (ODIH.CNCL_RTN_QTY * (CASE WHEN O.MALL_GB = 'G011_20' /*제휴몰*/ THEN
		                                                                IFNULL(EUP.USAC_PRICE,ODIH.ITEM_PRICE)
		                                                            ELSE
		                                                                ODIH.ITEM_PRICE
		                                                       END))
		                        END AS SIGNED INT))            AS AM_ACSALE /*실판매금액*/
		        FROM   TB_ORDER_DETAIL_ITEM_HST ODIH
		        INNER JOIN TB_ORDER_DETAIL OD ON ODIH.ORD_DTL_NO = OD.ORD_DTL_NO
		        INNER JOIN TB_ORDER O ON ODIH.ORD_NO = O.ORD_NO
		        INNER JOIN TB_SUPPLY_COMPANY SC ON OD.SUPPLY_COMP_CD = SC.SUPPLY_COMP_CD
		        INNER JOIN TB_GOODS G ON ODIH.ITEM_CD = G.GOODS_CD
		        LEFT OUTER JOIN TB_EXTMALL_USAC_PRICE EUP ON ODIH.ORD_DTL_ITEM_HST_SQ = EUP.ORD_DTL_ITEM_HST_SQ
		        WHERE  1 = 1
		        AND    ODIH.REG_DT <![CDATA[>=]]> DATE_ADD(CURRENT_DATE(), INTERVAL -1 DAY)
		        AND    ODIH.REG_DT <![CDATA[<]]> CURRENT_DATE()
		        AND    ODIH.ORD_DTL_STAT IN ('G720_20','G720_50','G720_60') /*판매-배송중, 환입-반품완료, 환입-교환완료*/
		        <if test='erpGb == "hsmk"'>
		        AND    OD.SUPPLY_COMP_CD = 5 /*한세MK*/
		        </if>
		        <if test='erpGb == "hsdr"'>
		        AND    OD.SUPPLY_COMP_CD = 3 /*한세드림*/
		        </if>
		        AND    SC.DISTRIBUTION_GB IN ('G065_10','G065_11','G065_12') /*자사*/
		        GROUP  BY DATE_FORMAT(ODIH.REG_DT,'%Y%m%d')
		                , CASE WHEN ODIH.ORD_DTL_STAT = 'G720_20' THEN '1'
		                       ELSE '2'
		                  END
		                , SUBSTRING(ODIH.SKU_MODEL_NO,1,10)
		                , SUBSTRING(ODIH.SKU_MODEL_NO,11,2)
		                , TRIM(SUBSTRING(ODIH.SKU_MODEL_NO,13,3))
		                , G.BRAND_CD
		       ) ODIH
		LEFT OUTER JOIN TB_HANSAE_STYLE HS ON ODIH.CD_STYLE = HS.CD_STYLE
		                                  AND ODIH.CD_COLOR = HS.CD_COLOR
		                                  AND ODIH.CD_SIZE = HS.CD_SIZE
		                                  AND HS.ERP_GB = #{erpGb}
		WHERE  ODIH.QT_SALE <![CDATA[<>]]> 0
		AND    NOT EXISTS (SELECT 1
		                   FROM   TB_HANSAE_SALES
		                   WHERE  ERP_GB = #{erpGb}
		                   AND    DT_SALE = ODIH.DT_SALE
		                   AND    TP_SALE = ODIH.TP_SALE
		                   AND    CD_STYLE = IFNULL(HS.ERP_CD_STYLE,ODIH.CD_STYLE)
		                   AND    CD_COLOR = IFNULL(HS.ERP_CD_COLOR,ODIH.CD_COLOR)
		                   AND    CD_SIZE = IFNULL(HS.ERP_CD_SIZE,ODIH.CD_SIZE)
		                  )
		GROUP  BY #{erpGb}
		        , ODIH.DT_SALE
		        , ODIH.TP_SALE
		        , IFNULL(HS.ERP_CD_STYLE,ODIH.CD_STYLE)
		        , IFNULL(HS.ERP_CD_COLOR,ODIH.CD_COLOR)
		        , IFNULL(HS.ERP_CD_SIZE,ODIH.CD_SIZE)
		        , ODIH.BRAND_CD
	</insert>
	
	<!-- 매출반영 브랜드 목록 -->
	<select id="getSalesUploadBrandList" parameterType="String" resultType="String">
		/* TsbStatistics.getSalesUploadBrandList */
		SELECT DISTINCT BRAND_CD
		FROM   TB_HANSAE_SALES
		WHERE  ERP_GB = #{erpGb}
		AND    DT_SALE = DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL -1 DAY),'%Y%m%d') /*전일에 등록된 넘*/
		AND    CD_SALEBILL IS NULL /*판매전표가없는넘(=매출반영안했거나 실패한넘)*/
		ORDER  BY BRAND_CD
	</select>
	
	<!-- 매출반영 브랜드 목록 -->
	<select id="getSalesUploadBrandList2" parameterType="Erp" resultType="String">
		/* TsbStatistics.getSalesUploadBrandList2 */
		SELECT DISTINCT BRAND_CD
		FROM   TB_HANSAE_SALES
		WHERE  ERP_GB = #{erpGb}
		AND    DT_SALE = #{dtSale}
		AND    CD_SALEBILL IS NULL /*판매전표가없는넘(=매출반영안했거나 실패한넘)*/
		ORDER  BY BRAND_CD
	</select>
	
	<!-- 매출반영 목록 -->
	<select id="getSalesUploadList" parameterType="Erp" resultType="paramMap">
		/* TsbStatistics.getSalesUploadList */
		SELECT DT_SALE
		     , TP_SALE
		     , CD_STYLE
		     , CD_COLOR
		     , CD_SIZE
		     , QT_SALE
		     , AM_ACSALE
		     , DS_REMARK
		FROM   TB_HANSAE_SALES
		WHERE  ERP_GB = #{erpGb}
		AND    DT_SALE = DATE_FORMAT(DATE_ADD(CURRENT_DATE(), INTERVAL -1 DAY),'%Y%m%d') /*전일에 등록된 넘*/
		AND    BRAND_CD = #{brandCd}
		AND    CD_SALEBILL IS NULL /*판매전표가없는넘(=매출반영안했거나 실패한넘)*/
		ORDER  BY DT_SALE, TP_SALE, CD_STYLE, CD_COLOR, CD_SIZE
	</select>
	
	<!-- 매출반영 목록 (판매일자 파라미터 추가용) -->
	<select id="getSalesUploadList2" parameterType="Erp" resultType="paramMap">
		/* TsbStatistics.getSalesUploadList2 */
		SELECT DT_SALE
		     , TP_SALE
		     , CD_STYLE
		     , CD_COLOR
		     , CD_SIZE
		     , QT_SALE
		     , AM_ACSALE
		     , DS_REMARK
		FROM   TB_HANSAE_SALES
		WHERE  ERP_GB = #{erpGb}
		AND    DT_SALE = #{dtSale}
		AND    BRAND_CD = #{brandCd}
		AND    CD_SALEBILL IS NULL /*판매전표가없는넘(=매출반영안했거나 실패한넘)*/
		ORDER  BY DT_SALE, TP_SALE, CD_STYLE, CD_COLOR, CD_SIZE
	</select>
	
	<!-- 매출반영I/F번호 조회 -->
	<select id="getSalesUploadInterfaceNo" parameterType="String" resultType="String">
		/* TsbStatistics.getSalesUploadInterfaceNo */
		SELECT CONCAT(DATE_FORMAT(NOW(),'%Y%m%d%H%i%S')
		             ,LPAD(IFNULL((SELECT CAST(MAX(SUBSTRING(NO_IF,15)) AS UNSIGNED)
		                           FROM   TB_HANSAE_SALES
		                           WHERE  ERP_GB = #{erpGb}
		                           AND    NO_IF LIKE CONCAT(DATE_FORMAT(NOW(),'%Y%m%d'),'%')
		                          ),0) + 1,4,'0')) AS NO_IF
		FROM   DUAL
	</select>
	
	<!-- 매출반영목록 I/F번호 Update -->
	<update id="updateSalesUploadListInterfaceNo" parameterType="paramMap">
		/* TsbStatistics.updateSalesUploadListInterfaceNo */
		UPDATE TB_HANSAE_SALES
		SET    NO_IF = #{NO_IF}
		WHERE  ERP_GB = #{ERP_GB}
		AND    DT_SALE = #{DT_SALE}
		AND    TP_SALE = #{TP_SALE}
		AND    CD_STYLE = #{CD_STYLE}
		AND    CD_COLOR = #{CD_COLOR}
		AND    CD_SIZE = #{CD_SIZE}
	</update>
	
	<!-- 매출반영결과 처리 -->
	<update id="updateSalesUploadResult" parameterType="paramMap">
		/* TsbStatistics.updateSalesUploadResult */
		UPDATE TB_HANSAE_SALES
		SET    CD_SALEBILL = #{CD_SALEBILL}
		     , DS_ERROR = #{DS_ERROR}
		     , NO_IF = CASE WHEN LENGTH(#{DS_ERROR}) > 0 THEN NULL ELSE #{NO_IF} END
		     , UPD_DT = NOW()
		WHERE  ERP_GB = #{ERP_GB}
		AND    NO_IF = #{NO_IF}
		AND    CD_STYLE = #{CD_STYLE}
		AND    CD_COLOR = #{CD_COLOR}
		AND    CD_SIZE = #{CD_SIZE}
	</update>

	<!-- 전체회원 -->
	<insert id="createStatCustAll" parameterType="String" timeout="6000">
		/* TsbStatistics.createStatCustAll */
		INSERT INTO TB_STAT_CUST_ALL (DAY, SEX_GB, AGE, CUST_STAT, CUST_GRADE, CUST_CNT)
		SELECT
			DATE_FORMAT(#{currDay},'%Y-%m-%d') AS DAY
			, IFNULL(FN_DEC_AES(SEX_GB), 'G007_X') AS SEX_GB
			, CASE WHEN FN_DEC_AES(BIRTH_YMD) = '' OR FN_DEC_AES(BIRTH_YMD) IS NULL THEN 'X'
			WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
			WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
			WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
			WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
			ELSE 'X' END                                                  AS AGE
			, CUST_STAT
			, CUST_GRADE
			, COUNT(1) AS CNT
		FROM TB_CUSTOMER
		WHERE CUST_NO > 0
			AND JOIN_DT >= DATE_FORMAT('20060101', '%Y%m%d')
			AND JOIN_DT &lt;= DATE_FORMAT(CONCAT(#{currDay},'235959'), '%Y%m%d%h%i%s')
		GROUP BY DAY, SEX_GB, AGE, CUST_STAT, CUST_GRADE
	</insert>

	<!-- 전체휴면회원 -->
	<insert id="createStatCustDorm" parameterType="String" timeout="6000">
		/* TsbStatistics.createStatCustDorm */
		INSERT INTO TB_STAT_CUST_DORM (DAY, SEX_GB, AGE, CUST_CNT)
		SELECT
			DAY
			, SEX_GB
			, AGE
			, SUM(CNT) AS CNT
		FROM (
			SELECT
				DATE_FORMAT(DORMANT_DT,'%Y-%m-%d') AS DAY
				, IFNULL(FN_DEC_AES(SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(BIRTH_YMD) = '' OR FN_DEC_AES(BIRTH_YMD) IS NULL THEN 'X'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
				ELSE 'X' END                                                  AS AGE
				, COUNT(1) AS CNT
			FROM TB_DORMANT_CUST
			WHERE CUST_NO > 0
				AND DORMANT_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')
				AND DORMANT_DT &lt;= DATE_FORMAT(CONCAT(#{currDay},'235959'), '%Y%m%d%h%i%s')
			GROUP BY DAY, SEX_GB, AGE
			UNION ALL
			SELECT
				DATE_FORMAT(RM_DORMANT_DT,'%Y-%m-%d') AS DAY
				, IFNULL(FN_DEC_AES(SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(BIRTH_YMD) = '' OR FN_DEC_AES(BIRTH_YMD) IS NULL THEN 'X'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
				ELSE 'X' END                                                  AS AGE
				, COUNT(1) * -1 AS CNT
			FROM TB_CUSTOMER
			WHERE CUST_NO > 0
				AND RM_DORMANT_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')
				AND RM_DORMANT_DT &lt;= DATE_FORMAT(CONCAT(#{currDay},'235959'), '%Y%m%d%h%i%s')
			GROUP BY DAY, SEX_GB, AGE
			UNION ALL
			SELECT
				DATE_FORMAT(#{currDay}, '%Y-%m-%d') AS DAY
				, SEX_GB
				, AGE
				,SUM(DORM_CNT) AS CNT
			FROM TB_STAT_CUST
			WHERE DAY = DATE_ADD(DATE_FORMAT(#{currDay}, '%Y%m%d'), INTERVAL -1 DAY)
			GROUP BY DAY, SEX_GB, AGE
		) Z
		GROUP BY DAY, SEX_GB, AGE
	</insert>

	<!-- 휴면탈퇴회원 -->
	<insert id="createStatCustExt" parameterType="String" timeout="6000">
		/* TsbStatistics.createStatCustExt */
		INSERT INTO TB_STAT_CUST_EXT (DAY, SEX_GB, AGE, GB, CUST_CNT)
		SELECT
			DATE_FORMAT(DORMANT_DT,'%Y-%m-%d') AS DAY
			, IFNULL(FN_DEC_AES(SEX_GB), 'G007_X') AS SEX_GB
			, CASE WHEN FN_DEC_AES(BIRTH_YMD) = '' OR FN_DEC_AES(BIRTH_YMD) IS NULL THEN 'X'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
			ELSE 'X' END                                                  AS AGE
			, 'A' AS GB
			, COUNT(1) AS CNT
		FROM TB_DORMANT_CUST
		WHERE CUST_NO > 0
			AND DORMANT_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')
			AND DORMANT_DT &lt;= DATE_FORMAT(CONCAT(#{currDay},'235959'), '%Y%m%d%h%i%s')
		GROUP BY DAY, SEX_GB, AGE
		UNION ALL
		SELECT
			DATE_FORMAT(RM_DORMANT_DT,'%Y-%m-%d') AS DAY
			, IFNULL(FN_DEC_AES(SEX_GB), 'G007_X') AS SEX_GB
			, CASE WHEN FN_DEC_AES(BIRTH_YMD) = '' OR FN_DEC_AES(BIRTH_YMD) IS NULL THEN 'X'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
			ELSE 'X' END                                                  AS AGE
			, 'B' AS GB
			, COUNT(1) AS CNT
		FROM TB_CUSTOMER
		WHERE CUST_NO > 0
			AND RM_DORMANT_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')
			AND RM_DORMANT_DT &lt;= DATE_FORMAT(CONCAT(#{currDay},'235959'), '%Y%m%d%h%i%s')
		GROUP BY DAY, SEX_GB, AGE
		UNION ALL
		SELECT
			DATE_FORMAT(SECEDE_DT,'%Y-%m-%d') AS DAY
			, IFNULL(FN_DEC_AES(SEX_GB), 'G007_X') AS SEX_GB
			, CASE WHEN FN_DEC_AES(BIRTH_YMD) = '' OR FN_DEC_AES(BIRTH_YMD) IS NULL THEN 'X'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
			ELSE 'X' END                                                  AS AGE
			, 'C' AS GB
			, COUNT(1) AS CNT
		FROM TB_SECEDE_CUST A
		WHERE CUST_NO > 0
		AND SECEDE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')
		AND SECEDE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay},'235959'), '%Y%m%d%h%i%s')
		GROUP BY DAY, SEX_GB, AGE
	</insert>

	<!-- 가입회원 -->
	<insert id="createStatCustJoin" parameterType="String" timeout="6000">
		/* TsbStatistics.createStatCustJoin */
		INSERT INTO TB_STAT_CUST_JOIN (DAY, SEX_GB, AGE, DIRECT_YN, CUST_CNT)
		SELECT
			DATE_FORMAT(JOIN_DT,'%Y-%m-%d') AS DAY
			, IFNULL(FN_DEC_AES(SEX_GB), 'G007_X') AS SEX_GB
			, CASE WHEN FN_DEC_AES(BIRTH_YMD) = '' OR FN_DEC_AES(BIRTH_YMD) IS NULL THEN 'X'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
				WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
			ELSE 'X' END                                                  AS AGE
			, CASE WHEN (SELECT COUNT(1) FROM TB_AF_LINK X WHERE X.AF_LINK_CD = A.AF_LINK_CD AND X.AF_CHANNEL = 'G053_01') > 0 THEN 'Y' ELSE 'N' END AS DIRECT_YN
			, COUNT(1) AS CNT
		FROM TB_CUSTOMER A
		WHERE CUST_NO > 0
		AND JOIN_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')
		AND JOIN_DT &lt;= DATE_FORMAT(CONCAT(#{currDay},'235959'), '%Y%m%d%h%i%s')
		GROUP BY DAY, SEX_GB, AGE, DIRECT_YN
	</insert>

	<!-- 회원통계 -->
	<insert id="createStatCust" parameterType="String" timeout="6000">
		/* TsbStatistics.createStatCust */
		INSERT INTO TB_STAT_CUST (DAY, SEX_GB, AGE, ALL_CNT, REAL_CNT, DORM_CNT, VIP_CNT, GOLD_CNT, SILVER_CNT, BRONZE_CNT, WELCOME_CNT, JOIN_CNT, DR_JOIN_CNT, AF_JOIN_CNT, CHG_DORM_CNT, RM_DORM_CNT, SECEED_CNT)
		SELECT
			DAY
			 , SEX_GB
			 , AGE
			 , IFNULL(SUM(CASE WHEN GB = '1' THEN CUST_CNT ELSE 0 END), 0) AS ALL_CNT
			 , IFNULL(SUM(CASE WHEN GB = '2' THEN CUST_CNT ELSE 0 END), 0) AS REAL_CNT
			 , IFNULL(SUM(CASE WHEN GB = '3' THEN CUST_CNT ELSE 0 END), 0) AS DORM_CNT
			 , IFNULL(SUM(CASE WHEN GB = '4' THEN CUST_CNT ELSE 0 END), 0) AS VIP_CNT
			 , IFNULL(SUM(CASE WHEN GB = '5' THEN CUST_CNT ELSE 0 END), 0) AS GOLD_CNT
			 , IFNULL(SUM(CASE WHEN GB = '6' THEN CUST_CNT ELSE 0 END), 0) AS SILVER_CNT
			 , IFNULL(SUM(CASE WHEN GB = '7' THEN CUST_CNT ELSE 0 END), 0) AS BRONZE_CNT
			 , IFNULL(SUM(CASE WHEN GB = '8' THEN CUST_CNT ELSE 0 END), 0) AS WELCOME_CNT
			 , IFNULL(SUM(CASE WHEN GB = '9' THEN CUST_CNT ELSE 0 END), 0) AS JOIN_CNT
			 , IFNULL(SUM(CASE WHEN GB = '10' THEN CUST_CNT ELSE 0 END), 0) AS DR_JOIN_CNT
			 , IFNULL(SUM(CASE WHEN GB = '11' THEN CUST_CNT ELSE 0 END), 0) AS AF_JOIN_CNT
			 , IFNULL(SUM(CASE WHEN GB = '12' THEN CUST_CNT ELSE 0 END), 0) AS CHG_DORM_CNT
			 , IFNULL(SUM(CASE WHEN GB = '13' THEN CUST_CNT ELSE 0 END), 0) AS RM_DORM_CNT
			 , IFNULL(SUM(CASE WHEN GB = '14' THEN CUST_CNT ELSE 0 END), 0) AS SECEED_CNT
		FROM (
				 -- 전체회원
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '1'                          AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_ALL
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- 실회원
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '2'                          AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_ALL
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND CUST_STAT = 'G104_10'
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- 휴면회원
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '3'                          AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_DORM
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- VIP
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '4'                          AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_ALL
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND CUST_STAT = 'G104_10'
				   AND CUST_GRADE = 'G110_10'
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- GOLD
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '5'                          AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_ALL
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND CUST_STAT = 'G104_10'
				   AND CUST_GRADE = 'G110_20'
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- SILVER
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '6'                          AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_ALL
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND CUST_STAT = 'G104_10'
				   AND CUST_GRADE = 'G110_30'
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- BRONZE
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '7'                          AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_ALL
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND CUST_STAT = 'G104_10'
				   AND CUST_GRADE = 'G110_40'
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- WELCOME
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '8'                          AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_ALL
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND CUST_STAT = 'G104_10'
				   AND CUST_GRADE = 'G110_50'
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- 가입수
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '9'                          AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_JOIN
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- 직방문가입수
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '10'                         AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_JOIN
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND DIRECT_YN = 'Y'
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- 제휴가입수
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '11'                         AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_JOIN
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND DIRECT_YN = 'N'
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- 휴면전환
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '12'                         AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_EXT
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND GB = 'A'
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- 휴면해제
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '13'                         AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_EXT
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND GB = 'B'
				 GROUP BY DAY, SEX_GB, AGE
				 UNION ALL
				 -- 탈퇴
				 SELECT DATE_FORMAT(DAY, '%Y-%m-%d') AS DAY
					  , '14'                         AS GB
					  , SEX_GB
					  , AGE
					  , SUM(CUST_CNT)                AS CUST_CNT
				 FROM TB_STAT_CUST_EXT
				 WHERE DAY = DATE_FORMAT(#{currDay}, '%Y-%m-%d')
				   AND GB = 'C'
				 GROUP BY DAY, SEX_GB, AGE
			 ) Z
		GROUP BY DAY, SEX_GB, AGE
	</insert>

	<!-- 회원주문통계 -->
	<insert id="createStatCustOrd" parameterType="String" timeout="600">
		/* TsbStatistics.createStatCustOrd */
		INSERT INTO TB_STAT_CUST_ORD (DAY, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE, SELL_QTY, ORD_AMT, CNCL_QTY, RTN_QTY, CNCL_AMT, RTN_AMT, ORD_CNT)
		WITH TAB AS (
			SELECT *
			FROM (
				SELECT A.ORD_NO
					, B.ORD_DTL_NO
					, B.ORD_QTY
					, B.CNCL_RTN_QTY
					, B.ORD_AMT
					, B.CNCL_RTN_AMT
					, B.CPN1_DC_AMT
					, A.PAY_DT
					, A.FRONT_GB
					, C.ITEMKIND_CD
					, C.BRAND_CD
					, IFNULL(FN_DEC_AES(E.SEX_GB), 'G007_X')                            AS SEX_GB
					, CASE WHEN FN_DEC_AES(E.BIRTH_YMD) = '' OR FN_DEC_AES(E.BIRTH_YMD) IS NULL THEN 'X'
						WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(E.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
						WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(E.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
						WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(E.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
						WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(E.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
					ELSE 'X' END                                                  AS AGE
					, E.CUST_NO
					, E.CUST_GRADE
					, RANK() OVER (PARTITION BY B.ORD_DTL_NO ORDER BY B.ORD_DTL_HST_SQ) AS RNK
				FROM TB_ORDER A,
					TB_ORDER_DETAIL_HST B,
					TB_GOODS C,
					TB_CUSTOMER E
				WHERE A.ORD_NO = B.ORD_NO
					AND B.GOODS_CD = C.GOODS_CD
					AND A.CUST_NO = E.CUST_NO
					AND A.PAY_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d')                         -- 시작일변수
					AND A.PAY_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
					AND A.MALL_GB = 'G011_10'
					AND B.ORD_EXCH_GB = 'O'
					AND B.ORD_DTL_STAT = 'G013_20' -- 결제완료
				) A
				WHERE RNK = 1
		)
		-- 판매수
		, TAB_SELL_QTY AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				, SUM(ORD_QTY) AS VAL
				, '1' AS GBN
				, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
			FROM TAB
			GROUP BY TERM, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
		)
		-- 매출
		, TAB_ORD AS (
			SELECT
				DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
				, SUM(ORD_AMT + CPN1_DC_AMT + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = A.ORD_DTL_NO)) AS VAL
				, '2' AS GBN
				, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
			FROM TAB A
			GROUP BY TERM, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
		)
		-- 취소갯수
		, TAB_CNCL_QTY AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d') AS TERM
				, SUM(CHG_QTY) AS VAL
				, '3' AS GBN
				, C.FRONT_GB, BRAND_CD, ITEMKIND_CD
				, IFNULL(FN_DEC_AES(G.SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(G.BIRTH_YMD) = '' OR FN_DEC_AES(G.BIRTH_YMD) IS NULL THEN 'X'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
				ELSE 'X' END                                                  AS AGE
				, CUST_GRADE
			FROM TB_ORDER_CHANGE_DETAIL A, TB_ORDER_DETAIL B, TB_ORDER C, TB_GOODS E, TB_CUSTOMER G
			WHERE A.ORD_DTL_NO = B.ORD_DTL_NO
				AND B.ORD_NO = C.ORD_NO
				AND B.GOODS_CD = E.GOODS_CD
				AND C.CUST_NO = G.CUST_NO
				AND CHG_STAT = 'G685_18' -- 결제후취소
				AND COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
				AND COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
				AND MALL_GB = 'G011_10'
			GROUP BY TERM, C.FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
		)
		-- 반품갯수
		, TAB_RTN_QTY AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d') AS TERM
				, SUM(CHG_QTY) AS VAL
				, '4' AS GBN
				, C.FRONT_GB, BRAND_CD, ITEMKIND_CD
				, IFNULL(FN_DEC_AES(G.SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(G.BIRTH_YMD) = '' OR FN_DEC_AES(G.BIRTH_YMD) IS NULL THEN 'X'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
				ELSE 'X' END                                                  AS AGE
				, CUST_GRADE
			FROM TB_ORDER_CHANGE_DETAIL A, TB_ORDER_DETAIL B, TB_ORDER C, TB_GOODS E, TB_CUSTOMER G
			WHERE A.ORD_DTL_NO = B.ORD_DTL_NO
				AND B.ORD_NO = C.ORD_NO
				AND B.GOODS_CD = E.GOODS_CD
				AND C.CUST_NO = G.CUST_NO
				AND CHG_STAT = 'G685_60' -- 반품완료
				AND COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
				AND COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
				AND MALL_GB = 'G011_10'
			GROUP BY TERM, C.FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
		)
		-- 취소액
		, TAB_CNCL_AMT AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d') AS TERM
				, SUM((C.CURR_PRICE + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = C.ORD_DTL_NO)) * B.CHG_QTY) AS VAL
				, '5' AS GBN
				, D.FRONT_GB, BRAND_CD, ITEMKIND_CD
				, IFNULL(FN_DEC_AES(G.SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(G.BIRTH_YMD) = '' OR FN_DEC_AES(G.BIRTH_YMD) IS NULL THEN 'X'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
				ELSE 'X' END                                                  AS AGE
				, CUST_GRADE
			FROM TB_ORDER_CHANGE_DETAIL B, TB_ORDER_DETAIL C, TB_ORDER D, TB_GOODS E, TB_CUSTOMER G
			WHERE B.ORD_DTL_NO = C.ORD_DTL_NO
				AND C.ORD_NO = D.ORD_NO
				AND C.GOODS_CD = E.GOODS_CD
				AND D.CUST_NO = G.CUST_NO
				AND CHG_STAT = 'G685_18' -- 결제후취소
				AND B.COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
				AND B.COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
				AND MALL_GB = 'G011_10'
			GROUP BY TERM, D.FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
		)
		-- 반품액
		, TAB_RTN_AMT AS (
			SELECT DATE_FORMAT(COMPLETE_DT, '%Y-%m-%d') AS TERM
				, SUM((C.CURR_PRICE + (SELECT SUM(OPT_ADD_PRICE * ITEM_QTY) FROM TB_ORDER_DETAIL_ITEM X WHERE X.ORD_DTL_NO = C.ORD_DTL_NO)) * B.CHG_QTY) AS VAL
				, '6' AS GBN
				, D.FRONT_GB, BRAND_CD, ITEMKIND_CD
				, IFNULL(FN_DEC_AES(G.SEX_GB), 'G007_X') AS SEX_GB
				, CASE WHEN FN_DEC_AES(G.BIRTH_YMD) = '' OR FN_DEC_AES(G.BIRTH_YMD) IS NULL THEN 'X'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 10 AND 19 THEN '10'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 20 AND 29 THEN '20'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 30 AND 39 THEN '30'
					WHEN (DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(CONCAT(SUBSTRING(FN_DEC_AES(G.BIRTH_YMD), 1, 4), '0101'), '%Y') + 1) BETWEEN 40 AND 49 THEN '40'
				ELSE 'X' END                                                  AS AGE
				, CUST_GRADE
			FROM TB_ORDER_CHANGE_DETAIL B, TB_ORDER_DETAIL C, TB_ORDER D, TB_GOODS E, TB_CUSTOMER G
			WHERE B.ORD_DTL_NO = C.ORD_DTL_NO
				AND C.ORD_NO = D.ORD_NO
				AND C.GOODS_CD = E.GOODS_CD
				AND D.CUST_NO = G.CUST_NO
				AND CHG_STAT = 'G685_60' -- 반품완료
				AND B.COMPLETE_DT >= DATE_FORMAT(#{currDay}, '%Y%m%d') -- 시작일변수
				AND B.COMPLETE_DT &lt;= DATE_FORMAT(CONCAT(#{currDay}, '235959'), '%Y%m%d%H%i%s') -- 종료일변수
				AND MALL_GB = 'G011_10'
			GROUP BY TERM, D.FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
		)
		-- 주문수
		, TAB_ORD_CNT AS (
			SELECT
				TERM
				, SUM(VAL) AS VAL
				, '7' AS GBN
				, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
			FROM (
				SELECT
					DATE_FORMAT(PAY_DT, '%Y-%m-%d') AS TERM
					, 1 AS VAL
					, ORD_NO
					, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
				FROM TAB
				GROUP BY TERM, ORD_NO, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
			) A
			GROUP BY TERM, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
		)
		SELECT
			TERM
			, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
			, SELL_QTY, ORD_AMT, CNCL_QTY, RTN_QTY, CNCL_AMT, RTN_AMT
			, ORD_CNT
		FROM (
			SELECT
				TERM
				, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
				, SUM(CASE WHEN GBN = '1' THEN VAL ELSE 0 END) AS SELL_QTY -- 판매수
				, SUM(CASE WHEN GBN = '2' THEN VAL ELSE 0 END) AS ORD_AMT -- 매출액(판매금액)
				, SUM(CASE WHEN GBN = '3' THEN VAL ELSE 0 END) AS CNCL_QTY -- 취소수량
				, SUM(CASE WHEN GBN = '4' THEN VAL ELSE 0 END) AS RTN_QTY -- 반품수량
				, SUM(CASE WHEN GBN = '5' THEN VAL ELSE 0 END) AS CNCL_AMT -- 취소금액
				, SUM(CASE WHEN GBN = '6' THEN VAL ELSE 0 END) AS RTN_AMT -- 반품금액
				, SUM(CASE WHEN GBN = '7' THEN VAL ELSE 0 END) AS ORD_CNT -- 주문수
			FROM (
				SELECT * FROM TAB_SELL_QTY
				UNION ALL
				SELECT * FROM TAB_ORD
				UNION ALL
				SELECT * FROM TAB_CNCL_QTY
				UNION ALL
				SELECT * FROM TAB_RTN_QTY
				UNION ALL
				SELECT * FROM TAB_CNCL_AMT
				UNION ALL
				SELECT * FROM TAB_RTN_AMT
				UNION ALL
				SELECT * FROM TAB_ORD_CNT
			) A
			GROUP BY TERM, FRONT_GB, BRAND_CD, ITEMKIND_CD, SEX_GB, AGE, CUST_GRADE
		) X
	</insert>
</mapper>