<?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="WebStatistics">

    <select id="selectTotalPaymentAmount" parameterType="hashmap" resultType="hashmap">
        SELECT MP.PREV_TOTAL_PAYMENT_AMOUNT  AS "prevTotalPaymentAmount"
              ,MP.THIS_TOTAL_PAYMENT_AMOUNT  AS "thisTotalPaymentAmount"
          FROM (
                SELECT (SELECT IFNULL(SUM(MP.PAYMENT_AMOUNT), 0)  AS TOTAL_PAYMENT_AMOUNT
                          FROM (
                                SELECT MPD.TOTAL_AMOUNT  AS PAYMENT_AMOUNT
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                            INNER JOIN (
                                        SELECT MP.PAYMENT_NUMBER
                                          FROM MU_PAYMENT AS MP
                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                           AND MP.USE_YN = 'Y'
                                           AND MPD.USE_YN = 'Y'
                                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                           AND MP.WRITE_DATE &gt;= #{prevYearMonth}
                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{prevYearMonth})
                                      GROUP BY MP.PAYMENT_NUMBER
                                  ) AS MP2
                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   <!-- AND MP.STATUS IN ('Y', 'R', 'NP', 'NY', 'P', 'PY') 결제완료, 환불, 미수, 미수결제, 부분결제, 부분결제(완료) -->
                                   AND MP.WRITE_DATE &gt;= #{prevYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{prevYearMonth})
                          ) AS MP)                                                            AS PREV_TOTAL_PAYMENT_AMOUNT
                      ,(SELECT IFNULL(SUM(MP.PAYMENT_AMOUNT), 0)  AS TOTAL_PAYMENT_AMOUNT
                          FROM (
                                SELECT MPD.TOTAL_AMOUNT  AS PAYMENT_AMOUNT
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                            INNER JOIN (
                                        SELECT MP.PAYMENT_NUMBER
                                          FROM MU_PAYMENT AS MP
                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                           AND MP.USE_YN = 'Y'
                                           AND MPD.USE_YN = 'Y'
                                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                      GROUP BY MP.PAYMENT_NUMBER
                                  ) AS MP2
                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   <!-- AND MP.STATUS IN ('Y', 'R', 'NP', 'NY', 'P', 'PY') 결제완료, 환불, 미수, 미수결제, 부분결제, 부분결제(완료) -->
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                          ) AS MP)                                                           AS THIS_TOTAL_PAYMENT_AMOUNT
          ) AS MP
    </select>

    <select id="selectTotalTreatmentCount" parameterType="hashmap" resultType="hashmap">
        SELECT MT.PREV_TOTAL_COUNT  AS "prevTotalTreatmentCount"
              ,MT.THIS_TOTAL_COUNT  AS "thisTotalTreatmentCount"
          FROM (
                SELECT (SELECT IFNULL(SUM(MT.COUNT), 0)   AS TOTAL_COUNT
                          FROM (
                                SELECT COUNT(MR.MU_TREATMENT_ID)          AS COUNT
                                  FROM MU_TREATMENT AS MT
                             LEFT JOIN (
                                        SELECT MR.MU_TREATMENT_ID
                                              ,MR.TREATMENT_NAME
                                          FROM (
                                                SELECT MP.MU_RESERVE_ID
                                                  FROM (
                                                        SELECT MPD.MU_RESERVE_ID         AS MU_RESERVE_ID
                                                          FROM MU_PAYMENT AS MP
                                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                           AND MP.USE_YN = 'Y'
                                                           AND MPD.USE_YN = 'Y'
                                                           AND MP.WRITE_DATE &gt;= #{prevYearMonth}
                                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{prevYearMonth})
                                                    INNER JOIN (
                                                                SELECT MP.PAYMENT_NUMBER
                                                                  FROM MU_PAYMENT AS MP
                                                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                                   AND MP.USE_YN = 'Y'
                                                                   AND MPD.USE_YN = 'Y'
                                                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                                   AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                                                   AND MP.WRITE_DATE &gt;= #{prevYearMonth}
                                                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{prevYearMonth})
                                                              GROUP BY MP.PAYMENT_NUMBER
                                                          ) AS MP2
                                                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                  ) AS MP
                                              GROUP BY MP.MU_RESERVE_ID
                                          ) AS MP
                                    INNER JOIN MU_RESERVE AS MR
                                            ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                                           AND MR.USE_YN = 'Y'
                             ) AS MR
                                    ON MT.MU_TREATMENT_ID = MR.MU_TREATMENT_ID
                                 WHERE MT.USE_YN = 'Y'
                              GROUP BY MT.MU_TREATMENT_ID
                                      ,MT.TREATMENT_NAME
                          ) AS MT)                                                                      AS PREV_TOTAL_COUNT
                      ,(SELECT IFNULL(SUM(MT.COUNT), 0)   AS TOTAL_COUNT
                          FROM (
                                SELECT COUNT(MR.MU_TREATMENT_ID)          AS COUNT
                                  FROM MU_TREATMENT AS MT
                             LEFT JOIN (
                                        SELECT MR.MU_TREATMENT_ID
                                              ,MR.TREATMENT_NAME
                                          FROM (
                                                SELECT MP.MU_RESERVE_ID
                                                  FROM (
                                                        SELECT MPD.MU_RESERVE_ID         AS MU_RESERVE_ID
                                                          FROM MU_PAYMENT AS MP
                                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                           AND MP.USE_YN = 'Y'
                                                           AND MPD.USE_YN = 'Y'
                                                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                                    INNER JOIN (
                                                                SELECT MP.PAYMENT_NUMBER
                                                                  FROM MU_PAYMENT AS MP
                                                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                                   AND MP.USE_YN = 'Y'
                                                                   AND MPD.USE_YN = 'Y'
                                                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                                   AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                                              GROUP BY MP.PAYMENT_NUMBER
                                                          ) AS MP2
                                                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                  ) AS MP
                                              GROUP BY MP.MU_RESERVE_ID
                                          ) AS MP
                                    INNER JOIN MU_RESERVE AS MR
                                            ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                                           AND MR.USE_YN = 'Y'
                             ) AS MR
                                    ON MT.MU_TREATMENT_ID = MR.MU_TREATMENT_ID
                                 WHERE MT.USE_YN = 'Y'
                              GROUP BY MT.MU_TREATMENT_ID
                                      ,MT.TREATMENT_NAME
                          ) AS MT)                                                                      AS THIS_TOTAL_COUNT
          ) MT
    </select>

    <select id="selectTotalTreatmentCountAmount" parameterType="hashmap" resultType="hashmap">
        SELECT MT.TREATMENT_NAME     AS "treatmentName"
              ,MT.ORDER_NUMBER       AS "orderNumber"
              ,SUM(MT.TOTAL_COUNT)   AS "totalCount"
              ,SUM(MT.TOTAL_AMOUNT)  AS "totalAmount"
          FROM (
                SELECT IF(MT.TREATMENT_NAME IN ('피부', '쁘띠', '비만', '제모')
                         ,MT.TREATMENT_NAME
                         ,'기타')                                                AS TREATMENT_NAME
                      ,IF(MT.TREATMENT_NAME IN ('피부', '쁘띠', '비만', '제모')
                         ,MT.ORDER_NUMBER
                         ,'5')                                                  AS ORDER_NUMBER
                      ,IFNULL(SUM(MR.COUNT), 0)                                 AS TOTAL_COUNT
                      ,IFNULL(SUM(MR.TOTAL_AMOUNT), 0)                          AS TOTAL_AMOUNT
                  FROM MU_TREATMENT AS MT
             LEFT JOIN (
                        SELECT MR.TREATMENT_NAME       AS TREATMENT_NAME
                              ,COUNT(*)                AS COUNT
                              ,SUM(MP.PAYMENT_AMOUNT)  AS TOTAL_AMOUNT
                          FROM (
                                SELECT MPD.MU_RESERVE_ID      AS MU_RESERVE_ID
                                      ,SUM(MPD.TOTAL_AMOUNT)  AS PAYMENT_AMOUNT
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MPD.MU_TREATMENT_ID != 'T172059821942700000000019'
                            INNER JOIN (
                                        SELECT MP.PAYMENT_NUMBER
                                          FROM MU_PAYMENT AS MP
                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                           AND MP.USE_YN = 'Y'
                                           AND MPD.USE_YN = 'Y'
                                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                      GROUP BY MP.PAYMENT_NUMBER
                                  ) AS MP2
                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MPD.MU_RESERVE_ID
                          ) AS MP
                    INNER JOIN MU_RESERVE AS MR
                            ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                           AND MR.USE_YN = 'Y'
                      GROUP BY MR.TREATMENT_NAME
             ) AS MR
                    ON MT.TREATMENT_NAME = MR.TREATMENT_NAME
                 WHERE MT.USE_YN = 'Y'
                   AND MT.MU_TREATMENT_ID != 'T172059821942700000000019'
              GROUP BY MT.TREATMENT_NAME
                      ,MT.ORDER_NUMBER
          ) AS MT
      GROUP BY MT.TREATMENT_NAME
              ,MT.ORDER_NUMBER
      ORDER BY MT.ORDER_NUMBER
    </select>

    <select id="selectTotalVisitorCount" parameterType="hashmap" resultType="hashmap">
        SELECT MP.PREV_TOTAL_COUNT    AS "prevTotalVisitorCount"
              ,MP.THIS_TOTAL_COUNT    AS "thisTotalVisitorCount"
          FROM (
                SELECT (SELECT COUNT(*)    AS TOTAL_COUNT
                          FROM (
                                SELECT MP.WRITE_DATE
                                      ,MP.MU_USER_ID
                                  FROM MU_PAYMENT AS MP
                                 WHERE MP.USE_YN = 'Y'
                                   AND MP.STATUS != 'C'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.WRITE_DATE &gt;= #{prevYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{prevYearMonth})
                              GROUP BY MP.WRITE_DATE
                                      ,MP.MU_USER_ID
                          ) AS MP)                                 AS PREV_TOTAL_COUNT
                      ,(SELECT COUNT(*)    AS TOTAL_COUNT
                          FROM (
                                SELECT MP.WRITE_DATE
                                      ,MP.MU_USER_ID
                                  FROM MU_PAYMENT AS MP
                                 WHERE MP.USE_YN = 'Y'
                                   AND MP.STATUS != 'C'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MP.WRITE_DATE
                                      ,MP.MU_USER_ID
                          ) AS MP)                                 AS THIS_TOTAL_COUNT
          ) AS MP
    </select>

    <select id="selectTotalVisitorGenderCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(CASE WHEN MP.GENDER = 'F' THEN 1 END)  AS "totalCountF"
              ,COUNT(CASE WHEN MP.GENDER = 'M' THEN 1 END)  AS "totalCountM"
          FROM (
                SELECT (SELECT MU.GENDER
                          FROM MU_USER AS MU
                         WHERE MU.USE_YN = 'Y'
                           AND MU.MU_USER_ID = MP.MU_USER_ID
                         LIMIT 0, 1)                            AS GENDER
                  FROM (
                        SELECT MP.WRITE_DATE
                              ,MP.MU_USER_ID
                          FROM MU_PAYMENT AS MP
                         WHERE MP.USE_YN = 'Y'
                           AND MP.STATUS != 'C'
                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                      GROUP BY MP.WRITE_DATE
                              ,MP.MU_USER_ID
                  ) AS MP
          ) AS MP
    </select>

    <select id="selectStatisticsMonthPayment" parameterType="hashmap" resultType="hashmap">
        SELECT
            TREATMENT_NAME                  AS treatmentName
          , SUM(PAYMENT_AMOUNT)             AS paymentAmount
        FROM (
              SELECT MPD.TREATMENT_NAME
                   , MP.PAYMENT_AMOUNT
              FROM MU_PAYMENT MP
                       JOIN (
                            SELECT PAYMENT_NUMBER
                                  , MU_PAYMENT_ID
                                  , MU_TREATMENT_PROCEDURE_ID
                                  , TREATMENT_PROCEDURE_NAME
                                  , MU_TREATMENT_ID
                                  , TREATMENT_NAME
                             FROM MU_PAYMENT_DETAIL
                             GROUP BY PAYMENT_NUMBER, MU_RESERVE_ID) MPD
                            ON MPD.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                                AND MP.STATUS != 'N'
              WHERE MP.PAYMENT_CATEGORY_ITEM_ID!='MM'
                AND MP.USE_YN = 'Y'
                AND WRITE_DATE &gt;= STR_TO_DATE(CONCAT(#{statisticsSearchDate}, '-01'), '%Y-%m-%d')
                AND WRITE_DATE &lt; DATE_FORMAT(STR_TO_DATE(CONCAT(#{statisticsSearchDate}, '-01'), '%Y-%m-%d') + INTERVAL 1 MONTH, '%Y-%m-%d')
              GROUP BY
                   MP.PAYMENT_NUMBER
                  ,MP.MU_PAYMENT_ID
                  ,MPD.MU_PAYMENT_ID) PA
        GROUP BY TREATMENT_NAME
    </select>

    <select id="selectStatisticsDailyPayment" parameterType="hashmap" resultType="hashmap">
        SELECT
            IFNULL(SUM(PAYMENT_AMOUNT), 0) AS paymentAmount
        FROM (
            SELECT MPD.TREATMENT_NAME
                   , MP.PAYMENT_AMOUNT
            FROM MU_PAYMENT MP
                       JOIN (SELECT PAYMENT_NUMBER
                                  , MU_PAYMENT_ID
                                  , MU_TREATMENT_PROCEDURE_ID
                                  , TREATMENT_PROCEDURE_NAME
                                  , MU_TREATMENT_ID
                                  , TREATMENT_NAME
                             FROM MU_PAYMENT_DETAIL
                             GROUP BY PAYMENT_NUMBER, MU_RESERVE_ID) MPD
                            ON MPD.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                                AND MP.STATUS != 'N'
              WHERE MP.PAYMENT_CATEGORY_ITEM_ID!='MM'
                AND MP.USE_YN = 'Y'
                AND WRITE_DATE &gt;= DATE_FORMAT(#{searchDate}, '%Y-%m-%d')
                AND WRITE_DATE &lt; DATE_FORMAT(#{searchDate}, '%Y-%m-%d') + INTERVAL 1 DAY
              GROUP BY
                  MP.PAYMENT_NUMBER
                  ,MP.MU_PAYMENT_ID
                  ,MPD.MU_PAYMENT_ID) PA
    </select>

    <select id="selectStatisticsPrevPayment" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT #{prevYearMonth} AS RESULT_DATE

            UNION ALL

            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
              FROM T_TEMP_DATES
             WHERE RESULT_DATE &lt; LAST_DAY(#{prevYearMonth})
        )
        SELECT TTD.RESULT_DATE                                            AS "resultDate"
              ,CONCAT(TTD.MONTH, '.', TTD.DAY, '(', TTD.DAY_OF_WEEK, ')') AS "date"
              ,IFNULL(MP.TOTAL_AMOUNT, 0)                                 AS "totalAmount"
          FROM (
                SELECT TTD.RESULT_DATE                  AS RESULT_DATE
                      ,MONTH(TTD.RESULT_DATE)           AS MONTH
                      ,DAY(TTD.RESULT_DATE)             AS DAY
                      ,CASE DAYOFWEEK(TTD.RESULT_DATE)
                       WHEN 1 THEN '일'
                       WHEN 2 THEN '월'
                       WHEN 3 THEN '화'
                       WHEN 4 THEN '수'
                       WHEN 5 THEN '목'
                       WHEN 6 THEN '금'
                       WHEN 7 THEN '토'
                       END                           AS DAY_OF_WEEK
                  FROM T_TEMP_DATES AS TTD
              ORDER BY RESULT_DATE ASC
          ) TTD
     LEFT JOIN (
                SELECT MP.WRITE_DATE
                      ,MP.TOTAL_AMOUNT
                  FROM (
                        SELECT MP.WRITE_DATE
                              ,IFNULL(SUM(MP.PAYMENT_AMOUNT), 0)  AS TOTAL_AMOUNT
                          FROM (
                                SELECT MPD.TOTAL_AMOUNT  AS PAYMENT_AMOUNT
                                      ,MP.WRITE_DATE     AS WRITE_DATE
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                            INNER JOIN (
                                        SELECT MP.PAYMENT_NUMBER
                                          FROM MU_PAYMENT AS MP
                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                           AND MP.USE_YN = 'Y'
                                           AND MPD.USE_YN = 'Y'
                                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                           AND MP.WRITE_DATE &gt;= #{prevYearMonth}
                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{prevYearMonth})
                                      GROUP BY MP.PAYMENT_NUMBER
                                  ) AS MP2
                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.WRITE_DATE &gt;= #{prevYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{prevYearMonth})
                          ) AS MP
                      GROUP BY MP.WRITE_DATE
                  ) AS MP
                 WHERE 1 = 1
          ) AS MP
            ON TTD.RESULT_DATE = MP.WRITE_DATE
    </select>

    <select id="selectStatisticsThisPayment" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT #{thisYearMonth} AS RESULT_DATE
    
            UNION ALL
    
            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
              FROM T_TEMP_DATES
             WHERE RESULT_DATE &lt; LAST_DAY(#{thisYearMonth})
        )
        SELECT TTD.RESULT_DATE                                            AS "resultDate"
              ,CONCAT(TTD.MONTH, '.', TTD.DAY, '(', TTD.DAY_OF_WEEK, ')') AS "date"
              ,IFNULL(MP.TOTAL_AMOUNT, 0)                                 AS "totalAmount"
          FROM (
                SELECT TTD.RESULT_DATE                  AS RESULT_DATE
                      ,MONTH(TTD.RESULT_DATE)           AS MONTH
                      ,DAY(TTD.RESULT_DATE)             AS DAY
                      ,CASE DAYOFWEEK(TTD.RESULT_DATE)
                       WHEN 1 THEN '일'
                       WHEN 2 THEN '월'
                       WHEN 3 THEN '화'
                       WHEN 4 THEN '수'
                       WHEN 5 THEN '목'
                       WHEN 6 THEN '금'
                       WHEN 7 THEN '토'
                       END                           AS DAY_OF_WEEK
                  FROM T_TEMP_DATES AS TTD
              ORDER BY RESULT_DATE ASC
          ) TTD
     LEFT JOIN (
                SELECT MP.WRITE_DATE
                      ,MP.TOTAL_AMOUNT
                  FROM (
                        SELECT MP.WRITE_DATE
                              ,IFNULL(SUM(MP.PAYMENT_AMOUNT), 0)  AS TOTAL_AMOUNT
                          FROM (
                                SELECT MPD.TOTAL_AMOUNT  AS PAYMENT_AMOUNT
                                      ,MP.WRITE_DATE     AS WRITE_DATE
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                            INNER JOIN (
                                        SELECT MP.PAYMENT_NUMBER
                                          FROM MU_PAYMENT AS MP
                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                           AND MP.USE_YN = 'Y'
                                           AND MPD.USE_YN = 'Y'
                                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                      GROUP BY MP.PAYMENT_NUMBER
                                  ) AS MP2
                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                          ) AS MP
                      GROUP BY MP.WRITE_DATE
                  ) AS MP
                 WHERE 1 = 1
          ) AS MP
            ON TTD.RESULT_DATE = MP.WRITE_DATE
    </select>
    <select id="selectListStatisticsDate" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT DATE(CONCAT(#{statisticsSearchDate}, '-01')) AS RESULT_DATE
        UNION ALL
        SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
        FROM T_TEMP_DATES
        WHERE RESULT_DATE &lt; LAST_DAY(CONCAT(#{statisticsSearchDate}, '-01'))
            )
        SELECT
            resultDate
            ,CONCAT(month,'.',day,'(',dayOfWeek,')') AS 'date'
        FROM
            (SELECT
            RESULT_DATE AS "resultDate",
            MONTH(RESULT_DATE) AS "month",
            DAY(RESULT_DATE) AS "day",
            CASE DAYOFWEEK(RESULT_DATE)
            WHEN 1 THEN '일'
            WHEN 2 THEN '월'
            WHEN 3 THEN '화'
            WHEN 4 THEN '수'
            WHEN 5 THEN '목'
            WHEN 6 THEN '금'
            WHEN 7 THEN '토'
        END AS "dayOfWeek"
         FROM T_TEMP_DATES
         ORDER BY RESULT_DATE) D
    </select>
    <select id="selectListStatisticsLastDate" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT DATE_SUB(DATE(CONCAT(#{statisticsSearchDate}, '-01')), INTERVAL 1 MONTH) AS RESULT_DATE
            UNION ALL
            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
            FROM T_TEMP_DATES
            WHERE RESULT_DATE &lt; LAST_DAY(DATE_SUB(CONCAT(#{statisticsSearchDate}, '-01'), INTERVAL 1 MONTH))
        )
        SELECT
            RESULT_DATE AS "resultDate",
            CONCAT(MONTH(RESULT_DATE), '.', DAY(RESULT_DATE), '(', CASE DAYOFWEEK(RESULT_DATE)
                WHEN 1 THEN '일'
                WHEN 2 THEN '월'
                WHEN 3 THEN '화'
                WHEN 4 THEN '수'
                WHEN 5 THEN '목'
                WHEN 6 THEN '금'
                WHEN 7 THEN '토'
            END, ')') AS 'date'
        FROM T_TEMP_DATES
        ORDER BY RESULT_DATE;
    </select>

    <select id="selectTotalPaymentAmountStatus" parameterType="hashmap" resultType="hashmap">
        SELECT IFNULL(SUM(MP.TOTAL_AMOUNT), 0)                  AS "paymentAmount"
              ,IFNULL(ABS(SUM(CASE
                              WHEN MP.STATUS IN ('C', 'CN', 'CY')
                              THEN MP.REFUND_TOTAL_AMOUNT
                               END)), 0)                        AS "cancelPaymentAmount"
              ,IFNULL(ABS(SUM(CASE
                              WHEN MP.STATUS IN ('R')
                              THEN MP.REFUND_TOTAL_AMOUNT
                               END)), 0)                        AS "refundPaymentAmount"
              ,IFNULL(SUM(CASE
                          WHEN MP.STATUS IN ('P', 'NP')
                          THEN MP.NON_PAYMENT_AMOUNT
                           END), 0)                             AS "nonPaymentAmount"
              ,IFNULL(SUM(MP.DISCOINT_AMOUNT), 0)               AS "discountAmount"
          FROM (
                SELECT MP.STATUS                   AS STATUS
                      ,SUM(MP.PAY_AMOUNT) AS TOTAL_AMOUNT
                      ,SUM(MP.REFUND_TOTAL_AMOUNT) AS REFUND_TOTAL_AMOUNT
                      ,SUM(IF(MP.STATUS IN ('NP', 'P')
                             ,MP.TOTAL_AMOUNT - MP.PAY_AMOUNT + MP.DISCOUNT_PRICE - MP.MEMBERSHIP_PRICE
                             ,0))                  AS NON_PAYMENT_AMOUNT
                      ,SUM(MP.DISCOUNT_PRICE)      AS DISCOINT_AMOUNT
                  FROM (
                        SELECT MP.PAYMENT_NUMBER                                                 AS PAYMENT_NUMBER
                              ,SUM(MP.PAY_AMOUNT)                                                AS PAY_AMOUNT
                              ,SUM(MP.DISCOUNT_PRICE)                                            AS DISCOUNT_PRICE
                              ,SUM(MP.MEMBERSHIP_PRICE)                                          AS MEMBERSHIP_PRICE
                              ,SUM(MP.REFUND_TOTAL_AMOUNT)                                       AS REFUND_TOTAL_AMOUNT
                              ,IFNULL((SELECT MP2.STATUS
                                         FROM MU_PAYMENT AS MP2
                                        WHERE MP2.USE_YN = 'Y'
                                          AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                                          AND MP2.WRITE_DATE &gt;= #{thisYearMonth}
                                          AND MP2.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                     ORDER BY MP2.REG_DATE DESC
                                        LIMIT 0, 1), '')                                          AS STATUS
                              ,SUM(MP.ORG_TREATMENT_PROCEDURE_PRICE + MP.ORG_TREATMENT_PROCEDURE_VAT)  AS TOTAL_AMOUNT
                          FROM (
                                SELECT IF(MP.STATUS IN ('R', 'C', 'CN'), 0, MP.PAY_AMOUNT)                   AS PAY_AMOUNT
                                      ,MP.DISCOUNT_PRICE                                                     AS DISCOUNT_PRICE
                                      ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, IFNULL(MP.MEMBERSHIP_PRICE, 0))  AS MEMBERSHIP_PRICE
                                      ,IF(MP.STATUS IN ('R', 'C', 'CN'), MP.PAY_AMOUNT, 0)                   AS REFUND_TOTAL_AMOUNT
                                      ,MP.PAYMENT_NUMBER                                                     AS PAYMENT_NUMBER
                                      ,MP.ORG_TREATMENT_PROCEDURE_PRICE                                      AS ORG_TREATMENT_PROCEDURE_PRICE
                                      ,MP.ORG_TREATMENT_PROCEDURE_VAT                                        AS ORG_TREATMENT_PROCEDURE_VAT
                                  FROM (
                                        SELECT MPD.TOTAL_AMOUNT                          AS PAY_AMOUNT
                                              ,MPD.DISCOUNT_PRICE                        AS DISCOUNT_PRICE
                                              ,MPD.MEMBERSHIP_PRICE                      AS MEMBERSHIP_PRICE
                                              ,MP.PAYMENT_NUMBER                         AS PAYMENT_NUMBER
                                              ,(SELECT MTPP.PRICE
                                                  FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                                 WHERE MTPP.USE_YN = 'Y'
                                                   AND MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                                                 LIMIT 0, 1)                             AS ORG_TREATMENT_PROCEDURE_PRICE
                                              ,(SELECT MTPP.VAT
                                                  FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                                 WHERE MTPP.USE_YN = 'Y'
                                                   AND MTPP.MU_TREATMENT_PROCEDURE_ID = MPD.MU_TREATMENT_PROCEDURE_ID
                                                 LIMIT 0, 1)                             AS ORG_TREATMENT_PROCEDURE_VAT
                                              ,MP.STATUS                                 AS STATUS
                                          FROM MU_PAYMENT AS MP
                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                           AND MP.USE_YN = 'Y'
                                           AND MPD.USE_YN = 'Y'
                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                    INNER JOIN (
                                                SELECT MP.PAYMENT_NUMBER
                                                  FROM MU_PAYMENT AS MP
                                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                   AND MP.USE_YN = 'Y'
                                                   AND MPD.USE_YN = 'Y'
                                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                   AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                              GROUP BY MP.PAYMENT_NUMBER
                                          ) AS MP2
                                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                  ) AS MP
                                 WHERE 1 = 1
                          ) AS MP
                         WHERE 1 = 1
                      GROUP BY MP.PAYMENT_NUMBER
                  ) AS MP
              GROUP BY MP.STATUS
          ) AS MP
         WHERE 1 = 1
    </select>

    <select id="selectTotalPaymentAmountCategory" parameterType="hashmap" resultType="hashmap">
        SELECT TEMP.PAYMENT_CATEGORY_ITEM_ID      AS "paymentCategoryItemId"
              ,TEMP.PAYMENT_CATEGORY_ITEM_NAME    AS "paymentCategoryItemName"
              ,IF(TEMP.PAYMENT_CATEGORY_ITEM_ID = 'MM'
                 ,IFNULL(ABS(MP.MEMBERSHIP_PRICE), 0)
                 ,IFNULL(ABS(MP.TOTAL_AMOUNT), 0))     AS "totalAmount"
          FROM (
                SELECT 'C'  AS PAYMENT_CATEGORY_ITEM_ID
                      ,'카드' AS PAYMENT_CATEGORY_ITEM_NAME
                UNION ALL
                SELECT 'M'  AS PAYMENT_CATEGORY_ITEM_ID
                      ,'현금' AS PAYMENT_CATEGORY_ITEM_NAME
                UNION ALL
                SELECT 'A'  AS PAYMENT_CATEGORY_ITEM_ID
                      ,'계좌이체' AS PAYMENT_CATEGORY_ITEM_NAME
                UNION ALL
                SELECT 'Q'  AS PAYMENT_CATEGORY_ITEM_ID
                      ,'간편결제' AS PAYMENT_CATEGORY_ITEM_NAME
                UNION ALL
                SELECT 'MM'  AS PAYMENT_CATEGORY_ITEM_ID
                      ,'멤버십' AS PAYMENT_CATEGORY_ITEM_NAME
                UNION ALL
                SELECT 'PP'  AS PAYMENT_CATEGORY_ITEM_ID
                      ,'선결제' AS PAYMENT_CATEGORY_ITEM_NAME
          ) AS TEMP
     LEFT JOIN (
                SELECT MP.PAYMENT_CATEGORY_ITEM_ID     AS PAYMENT_CATEGORY_ITEM_ID
                      ,MP.PAYMENT_CATEGORY_ITEM_NAME   AS PAYMENT_CATEGORY_ITEM_NAME
                      ,SUM(MP.TOTAL_AMOUNT)           AS TOTAL_AMOUNT
                      ,SUM(MP.MEMBERSHIP_PRICE)       AS MEMBERSHIP_PRICE
                  FROM (
                        SELECT MP.PAYMENT_CATEGORY_ITEM_ID     AS PAYMENT_CATEGORY_ITEM_ID
                              ,MP.PAYMENT_CATEGORY_ITEM_NAME   AS PAYMENT_CATEGORY_ITEM_NAME
                              ,MPD.TOTAL_AMOUNT           AS TOTAL_AMOUNT
                              ,MPD.MEMBERSHIP_PRICE       AS MEMBERSHIP_PRICE
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                           AND MP.STATUS IN ('Y', 'P', 'PY', 'NY', 'CY')
                    INNER JOIN (
                                SELECT MP.PAYMENT_NUMBER
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MP.STATUS IN ('Y', 'P', 'PY', 'NY', 'CY')
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MP.PAYMENT_NUMBER
                          ) AS MP2
                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                  ) AS MP
              GROUP BY MP.PAYMENT_CATEGORY_ITEM_ID
                      ,MP.PAYMENT_CATEGORY_ITEM_NAME
     ) AS MP
            ON TEMP.PAYMENT_CATEGORY_ITEM_ID = MP.PAYMENT_CATEGORY_ITEM_ID
           AND TEMP.PAYMENT_CATEGORY_ITEM_NAME = MP.PAYMENT_CATEGORY_ITEM_NAME
    </select>

    <select id="selectListStatisticsPayment" parameterType="hashmap" resultType="hashmap">
        SELECT *
          FROM (
                SELECT MP.PAYMENT_NUMBER                                                  AS "paymentNumber"
                      ,MP.MAX_RESERVE_DATE                                                AS "reserveDate"
                      ,MP.MAX_PAYMENT_DATE                                                AS "paymentDate"
                      ,IF(MP.PAYMENT_CATEGORY_ITEM_NAME = ''
                         ,IF(DISCOUNT_PRICE = MP.ORG_TREATMENT_PROCEDURE_PRICE+MP.ORG_TREATMENT_PROCEDURE_VAT
                            ,'-'
                            ,'미납')
                         ,REGEXP_REPLACE(REGEXP_REPLACE(MP.PAYMENT_CATEGORY_ITEM_NAME
                                                        ,'([^,]+)(,\\1)+'
                                                        ,'\\1')
                                        ,',+'
                                        ,','))                                            AS "paymentCategoryItemName"
                      ,MP.TREATMENT_NAME                                                  AS "treatmentName"
                      ,MP.TREATMENT_PROCEDURE_NAME                                        AS "treatmentProcedureNm"
                      ,MP.RESERVE_MEMBER_NAME                                             AS "reserveMemberName"
                      ,MP.MU_USER_ID                                                      AS "muUserId"
                      ,MP.USER_NAME                                                       AS "userName"
                      ,MP.MEMBER_NAME                                                     AS "muMemberName"
                      ,MP.ETC                                                             AS "etc"
                      ,MP.ORG_TREATMENT_PROCEDURE_PRICE+MP.ORG_TREATMENT_PROCEDURE_VAT    AS "totalAmount"
                      ,MP.DISCOUNT_PRICE                                                  AS "discountAmount"
                      ,MP.MEMBERSHIP_PRICE + MP.REFUND_MEMBERSHIP_PRICE                   AS "membershipPrice"
                      ,IF(MP.STATUS IN ('NP', 'P', 'NY', 'PY')
                         ,MP.ORG_TREATMENT_PROCEDURE_PRICE+MP.ORG_TREATMENT_PROCEDURE_VAT - MP.PAY_AMOUNT + MP.DISCOUNT_PRICE - MP.MEMBERSHIP_PRICE
                         ,0)                                                              AS "nonpaymentAmount"
                      ,MP.PAY_AMOUNT                                                      AS "paymentAmount"
                      ,MP.AMOUNT                                                          AS "amount"
                      ,MP.VAT                                                             AS "vat"
                      ,MP.REFUND_TOTAL_AMOUNT                                             AS "refundAmount"
                      ,CASE
                       WHEN MP.STATUS = 'C'  THEN '결제취소'
                       WHEN MP.STATUS = 'R'  THEN '환불'
                       WHEN MP.STATUS = 'PY' THEN '부분결제(완료)'
                       WHEN MP.STATUS = 'NY' THEN '미수결제'
                       WHEN MP.STATUS = 'CN' THEN '재결제(취소)'
                       WHEN MP.STATUS = 'CY' THEN '재결제(완료)'
                       WHEN MP.STATUS = 'Y'  THEN '결제완료'
                       WHEN MP.STATUS = 'P'  THEN '부분결제'
                       WHEN MP.STATUS = 'NP' THEN '미수'
                       ELSE ''
                        END                                                               AS "status1"
                      ,MP.MAX_REG_DATE                                                    AS "regDate"
                  FROM (
                        SELECT MP.PAYMENT_NUMBER                                                       AS PAYMENT_NUMBER
                              ,GROUP_CONCAT(DISTINCT MP.MAX_RESERVE_DATE)                              AS MAX_RESERVE_DATE
                              ,GROUP_CONCAT(DISTINCT MP.MAX_PAYMENT_DATE)                              AS MAX_PAYMENT_DATE
                              ,GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME)                    AS PAYMENT_CATEGORY_ITEM_NAME
                              ,GROUP_CONCAT(DISTINCT MP.MU_TREATMENT_ID)                               AS MU_TREATMENT_ID
                              ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                                AS TREATMENT_NAME
                              ,GROUP_CONCAT(MP.MU_TREATMENT_PROCEDURE_ID)                              AS MU_TREATMENT_PROCEDURE_ID
                              ,GROUP_CONCAT(MP.TREATMENT_PROCEDURE_NAME)                               AS TREATMENT_PROCEDURE_NAME
                              ,GROUP_CONCAT(DISTINCT MP.RESERVE_MEMBER_NAME)                           AS RESERVE_MEMBER_NAME
                              ,GROUP_CONCAT(DISTINCT MP.MU_USER_ID)                                    AS MU_USER_ID
                              ,GROUP_CONCAT(DISTINCT MP.USER_NAME)                                     AS USER_NAME
                              ,GROUP_CONCAT(DISTINCT MP.MEMBER_NAME)                                   AS MEMBER_NAME
                              ,GROUP_CONCAT(DISTINCT MP.ETC)                                           AS ETC
                              ,SUM(MP.PAY_AMOUNT)                                                      AS PAY_AMOUNT
                              ,SUM(MP.DISCOUNT_PRICE)                                                  AS DISCOUNT_PRICE
                              ,SUM(MP.MEMBERSHIP_PRICE)                                                AS MEMBERSHIP_PRICE
                              ,SUM(MP.AMOUNT)                                                          AS AMOUNT
                              ,SUM(MP.VAT)                                                             AS VAT
                              ,SUM(MP.REFUND_TOTAL_AMOUNT)                                             AS REFUND_TOTAL_AMOUNT
                              ,SUM(MP.REFUND_MEMBERSHIP_PRICE)                                         AS REFUND_MEMBERSHIP_PRICE
                              ,IFNULL((SELECT MP2.STATUS
                                         FROM MU_PAYMENT AS MP2
                                        WHERE MP2.USE_YN = 'Y'
                                          AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                                       <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                          AND MP2.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                          AND MP2.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                       </if>
                                       <if test="muUserId != null and muUserId != ''">
                                          AND MP.MU_USER_ID = #{muUserId}
                                       </if>
                                     ORDER BY MP2.REG_DATE DESC
                                        LIMIT 0, 1), '')                                               AS STATUS
                              ,GROUP_CONCAT(DISTINCT MP.MAX_REG_DATE)                                  AS MAX_REG_DATE
                              ,SUM((SELECT MTPP.PRICE
                                      FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                     WHERE MTPP.USE_YN = 'Y'
                                       AND MTPP.MU_TREATMENT_PROCEDURE_ID = MP.MU_TREATMENT_PROCEDURE_ID
                                     LIMIT 0, 1))                                                      AS ORG_TREATMENT_PROCEDURE_PRICE
                              ,SUM((SELECT MTPP.VAT
                                      FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                     WHERE MTPP.USE_YN = 'Y'
                                       AND MTPP.MU_TREATMENT_PROCEDURE_ID = MP.MU_TREATMENT_PROCEDURE_ID
                                     LIMIT 0, 1))                                                      AS ORG_TREATMENT_PROCEDURE_VAT
                          FROM (
                                SELECT MP.PAYMENT_NUMBER                                                 AS PAYMENT_NUMBER
                                      ,MP.MU_USER_ID                                                     AS MU_USER_ID
                                      ,MP.USER_NAME                                                      AS USER_NAME
                                      ,MAX(MP.RESERVE_DATE)                                              AS MAX_RESERVE_DATE
                                      ,MAX(MP.PAYMENT_DATE)                                              AS MAX_PAYMENT_DATE
                                      ,GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME)              AS PAYMENT_CATEGORY_ITEM_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.MU_TREATMENT_ID)                         AS MU_TREATMENT_ID
                                      ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                          AS TREATMENT_NAME
                                      ,MP.MU_TREATMENT_PROCEDURE_ID                                      AS MU_TREATMENT_PROCEDURE_ID
                                      ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ') AS TREATMENT_PROCEDURE_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.RESERVE_MEMBER_NAME SEPARATOR ', ')      AS RESERVE_MEMBER_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.MEMBER_NAME SEPARATOR ', ')              AS MEMBER_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.ETC)                                     AS ETC
                                      ,SUM(MP.PAY_AMOUNT)                                                AS PAY_AMOUNT
                                      ,SUM(MP.DISCOUNT_PRICE)                                            AS DISCOUNT_PRICE
                                      ,SUM(MP.MEMBERSHIP_PRICE)                                          AS MEMBERSHIP_PRICE
                                      ,SUM(MP.AMOUNT)                                                    AS AMOUNT
                                      ,SUM(MP.VAT)                                                       AS VAT
                                      ,SUM(MP.REFUND_TOTAL_AMOUNT)                                       AS REFUND_TOTAL_AMOUNT
                                      ,SUM(MP.REFUND_MEMBERSHIP_PRICE)                                   AS REFUND_MEMBERSHIP_PRICE
                                      ,MAX(MP.REG_DATE)                                                  AS MAX_REG_DATE
                                  FROM (
                                        SELECT MP.RESERVE_DATE                                                       AS RESERVE_DATE
                                              ,MP.PAYMENT_DATE                                                       AS PAYMENT_DATE
                                              ,IF(MP.STATUS IN ('NP')
                                                 ,NULL
                                                 ,MP.PAYMENT_CATEGORY_ITEM_NAME)                                     AS PAYMENT_CATEGORY_ITEM_NAME
                                              ,MP.MU_USER_ID                                                         AS MU_USER_ID
                                              ,MP.USER_NAME                                                          AS USER_NAME
                                              ,MP.MU_TREATMENT_ID                                                    AS MU_TREATMENT_ID
                                              ,MP.TREATMENT_NAME                                                     AS TREATMENT_NAME
                                              ,MP.MU_TREATMENT_PROCEDURE_ID                                          AS MU_TREATMENT_PROCEDURE_ID
                                              ,MP.TREATMENT_PROCEDURE_NAME                                           AS TREATMENT_PROCEDURE_NAME
                                              ,MP.RESERVE_MEMBER_ID                                                  AS RESERVE_MEMBER_ID
                                              ,MP.RESERVE_MEMBER_NAME                                                AS RESERVE_MEMBER_NAME
                                              ,MP.MU_MEMBER_ID                                                       AS MU_MEMBER_ID
                                              ,MP.MEMBER_NAME                                                        AS MEMBER_NAME
                                              ,NULLIF(MP.ETC, '')                                                    AS ETC
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, MP.PAY_AMOUNT)                   AS PAY_AMOUNT
                                              ,MP.DISCOUNT_PRICE                                                     AS DISCOUNT_PRICE
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, IFNULL(MP.MEMBERSHIP_PRICE, 0))  AS MEMBERSHIP_PRICE
                                              ,IF(MP.STATUS IN ('R', 'C'), 0, MP.AMOUNT)                             AS AMOUNT
                                              ,IF(MP.STATUS IN ('R', 'C'), 0, MP.VAT)                                AS VAT
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), MP.PAY_AMOUNT, 0)                   AS REFUND_TOTAL_AMOUNT
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), IFNULL(MP.MEMBERSHIP_PRICE, 0), 0)  AS REFUND_MEMBERSHIP_PRICE
                                              ,MP.PAYMENT_NUMBER                                                     AS PAYMENT_NUMBER
                                              ,MP.STATUS                                                             AS STATUS
                                              ,MP.REG_DATE                                                           AS REG_DATE
                                              ,MP.MU_RESERVE_ID                                                      AS MU_RESERVE_ID
                                          FROM (
                                                SELECT IFNULL(DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d'), '-')   AS RESERVE_DATE
                                                      ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')                  AS PAYMENT_DATE
                                                      ,MP.PAYMENT_CATEGORY_ITEM_NAME                           AS PAYMENT_CATEGORY_ITEM_NAME
                                                      ,MP.MU_USER_ID                                           AS MU_USER_ID
                                                      ,(SELECT MU.USER_NAME
                                                          FROM MU_USER AS MU
                                                         WHERE MU.USE_YN = 'Y'
                                                           AND MU.MU_USER_ID = MP.MU_USER_ID
                                                         LIMIT 0, 1)                                           AS USER_NAME
                                                      ,MPD.MU_TREATMENT_ID                                     AS MU_TREATMENT_ID
                                                      ,MPD.TREATMENT_NAME                                      AS TREATMENT_NAME
                                                      ,MPD.MU_TREATMENT_PROCEDURE_ID                           AS MU_TREATMENT_PROCEDURE_ID
                                                      ,MPD.TREATMENT_PROCEDURE_NAME                            AS TREATMENT_PROCEDURE_NAME
                                                      ,MPD.RESERVE_MEMBER_ID                                   AS RESERVE_MEMBER_ID
                                                      ,(SELECT MM.NAME
                                                          FROM MU_MEMBER AS MM
                                                         WHERE MM.USE_YN = 'Y'
                                                           AND MM.MU_MEMBER_ID = MPD.RESERVE_MEMBER_ID
                                                         LIMIT 0, 1)                                           AS RESERVE_MEMBER_NAME
                                                      ,MPD.MU_MEMBER_ID                                        AS MU_MEMBER_ID
                                                      ,(SELECT MM.NAME
                                                          FROM MU_MEMBER AS MM
                                                         WHERE MM.USE_YN = 'Y'
                                                           AND MM.MU_MEMBER_ID = MPD.MU_MEMBER_ID
                                                         LIMIT 0, 1)                                           AS MEMBER_NAME
                                                      ,MP.ETC                                    AS ETC
                                                      ,MPD.TOTAL_AMOUNT                          AS PAY_AMOUNT
                                                      ,MPD.DISCOUNT_PRICE                        AS DISCOUNT_PRICE
                                                      ,MPD.MEMBERSHIP_PRICE                      AS MEMBERSHIP_PRICE
                                                      ,MPD.AMOUNT                                AS AMOUNT
                                                      ,MPD.VAT                                   AS VAT
                                                      ,MP.PAYMENT_NUMBER                         AS PAYMENT_NUMBER
                                                      ,MP.STATUS                                 AS STATUS
                                                      ,MP.REG_DATE                               AS REG_DATE
                                                      ,MPD.MU_RESERVE_ID                         AS MU_RESERVE_ID
                                                  FROM MU_PAYMENT AS MP
                                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                   AND MP.USE_YN = 'Y'
                                                   AND MPD.USE_YN = 'Y'
                                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                            INNER JOIN (
                                                        SELECT MP.PAYMENT_NUMBER
                                                          FROM MU_PAYMENT AS MP
                                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                           AND MP.USE_YN = 'Y'
                                                           AND MPD.USE_YN = 'Y'
                                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                                      GROUP BY MP.PAYMENT_NUMBER
                                                  ) AS MP2
                                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                             LEFT JOIN MU_RESERVE AS MR
                                                    ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                          ) AS MP
                                         WHERE 1 = 1
                                  ) AS MP
                                 WHERE 1 = 1
                              GROUP BY MP.PAYMENT_NUMBER
                                      ,MP.MU_USER_ID
                                      ,MP.USER_NAME
                                      ,MP.MU_RESERVE_ID
                                      ,MP.MU_TREATMENT_PROCEDURE_ID
                          ) AS MP
                         WHERE 1 = 1
                      GROUP BY MP.PAYMENT_NUMBER
                  ) AS MP
                 WHERE 1 = 1
          ) AS MP
         WHERE 1 = 1
        <choose>
        <when test="sort != null and sort != ''">
      ORDER BY ${sort}
        </when>
        <otherwise>
      ORDER BY paymentDate DESC
              ,regDate DESC
        </otherwise>
        </choose>
    </select>

    <select id="selectTotalPaymentAmountTreatment" parameterType="hashmap" resultType="hashmap">
        SELECT MT.TREATMENT_NAME     AS "treatmentName"
              ,MT.ORDER_NUMBER       AS "orderNumber"
              ,SUM(MT.TOTAL_COUNT)   AS "totalCount"
              ,SUM(MT.TOTAL_AMOUNT)  AS "totalAmount"
          FROM (
                SELECT IF(MT.TREATMENT_NAME IN ('피부', '쁘띠', '비만', '제모')
                         ,MT.TREATMENT_NAME
                         ,'기타')                                                AS TREATMENT_NAME
                      ,IF(MT.TREATMENT_NAME IN ('피부', '쁘띠', '비만', '제모')
                         ,MT.ORDER_NUMBER
                         ,'5')                                                  AS ORDER_NUMBER
                      ,IFNULL(SUM(MR.COUNT), 0)                                 AS TOTAL_COUNT
                      ,IFNULL(SUM(MR.TOTAL_AMOUNT), 0)                          AS TOTAL_AMOUNT
                  FROM MU_TREATMENT AS MT
             LEFT JOIN (
                        SELECT MR.TREATMENT_NAME
                              ,COUNT(*)   AS COUNT
                              ,SUM(IFNULL(MP.TOTAL_AMOUNT, 0)) AS TOTAL_AMOUNT
                          FROM (
                                SELECT MR.MU_RESERVE_ID
                                      ,MR.TREATMENT_NAME
                                  FROM MU_RESERVE AS MR
                                 WHERE MR.USE_YN = 'Y'
                                   AND MR.CRM_MEMBER_SCHEDULE_PID IS NULL
                                   AND MR.RESERVE_DATE &gt;= #{thisYearMonth}
                                   AND MR.RESERVE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                   AND MR.STATUS IN ('Y')
                          ) AS MR
                     LEFT JOIN (
                                SELECT MPD.MU_RESERVE_ID AS MU_RESERVE_ID
                                      ,SUM(IFNULL(MPD.TOTAL_AMOUNT, 0))  AS TOTAL_AMOUNT
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MPD.MU_TREATMENT_ID != 'T172059821942700000000019'
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MPD.MU_RESERVE_ID
                          ) AS MP
                            ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                      GROUP BY MR.TREATMENT_NAME
                  ) AS MR
                    ON MT.TREATMENT_NAME = MR.TREATMENT_NAME
                 WHERE MT.USE_YN = 'Y'
                   AND MT.MU_TREATMENT_ID != 'T172059821942700000000019'
              GROUP BY MT.TREATMENT_NAME
                      ,MT.ORDER_NUMBER
          ) AS MT
      GROUP BY MT.TREATMENT_NAME
              ,MT.ORDER_NUMBER
      ORDER BY MT.ORDER_NUMBER
            
            
<!--        SELECT MT.TREATMENT_NAME     AS "treatmentName"
              ,MT.ORDER_NUMBER       AS "orderNumber"
              ,SUM(MT.TOTAL_COUNT)   AS "totalCount"
              ,SUM(MT.TOTAL_AMOUNT)  AS "totalAmount"
          FROM (
                SELECT IF(MT.TREATMENT_NAME IN ('피부', '쁘띠', '비만', '제모')
                         ,MT.TREATMENT_NAME
                         ,'기타')                                                AS TREATMENT_NAME
                      ,IF(MT.TREATMENT_NAME IN ('피부', '쁘띠', '비만', '제모')
                         ,MT.ORDER_NUMBER
                         ,'5')                                                  AS ORDER_NUMBER
                      ,IFNULL(SUM(MR.COUNT), 0)                                 AS TOTAL_COUNT
                      ,IFNULL(SUM(MR.TOTAL_AMOUNT), 0)                          AS TOTAL_AMOUNT
                  FROM MU_TREATMENT AS MT
             LEFT JOIN (
                        SELECT MR.TREATMENT_NAME       AS TREATMENT_NAME
                              ,COUNT(*)                AS COUNT
                              ,SUM(MP.PAYMENT_AMOUNT)  AS TOTAL_AMOUNT
                          FROM (
                                SELECT MPD.MU_RESERVE_ID      AS MU_RESERVE_ID
                                      ,SUM(MPD.TOTAL_AMOUNT)  AS PAYMENT_AMOUNT
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MPD.MU_TREATMENT_ID != 'T172059821942700000000019'
                            INNER JOIN (
                                        SELECT MP.PAYMENT_NUMBER
                                          FROM MU_PAYMENT AS MP
                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                           AND MP.USE_YN = 'Y'
                                           AND MPD.USE_YN = 'Y'
                                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                      GROUP BY MP.PAYMENT_NUMBER
                                  ) AS MP2
                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MPD.MU_RESERVE_ID
                          ) AS MP
                    INNER JOIN MU_RESERVE AS MR
                            ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                           AND MR.USE_YN = 'Y'
                      GROUP BY MR.TREATMENT_NAME
             ) AS MR
                    ON MT.TREATMENT_NAME = MR.TREATMENT_NAME
                 WHERE MT.USE_YN = 'Y'
                   AND MT.MU_TREATMENT_ID != 'T172059821942700000000019'
              GROUP BY MT.TREATMENT_NAME
                      ,MT.ORDER_NUMBER
          ) AS MT
      GROUP BY MT.TREATMENT_NAME
              ,MT.ORDER_NUMBER
      ORDER BY MT.ORDER_NUMBER -->
    </select>

    <select id="selectStatisticsThisPaymentTreatment" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT #{thisYearMonth} AS RESULT_DATE

            UNION ALL

            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
              FROM T_TEMP_DATES
             WHERE RESULT_DATE &lt; LAST_DAY(#{thisYearMonth})
        )
        SELECT TTD.RESULT_DATE                                            AS "resultDate"
              ,CONCAT(TTD.MONTH, '.', TTD.DAY, '(', TTD.DAY_OF_WEEK, ')') AS "date"
              ,IFNULL(MP.TOTAL_AMOUNT, 0)                                 AS "totalAmount"
          FROM (
                SELECT TTD.RESULT_DATE                  AS RESULT_DATE
                      ,MONTH(TTD.RESULT_DATE)           AS MONTH
                      ,DAY(TTD.RESULT_DATE)             AS DAY
                      ,CASE DAYOFWEEK(TTD.RESULT_DATE)
                       WHEN 1 THEN '일'
                       WHEN 2 THEN '월'
                       WHEN 3 THEN '화'
                       WHEN 4 THEN '수'
                       WHEN 5 THEN '목'
                       WHEN 6 THEN '금'
                       WHEN 7 THEN '토'
                       END                           AS DAY_OF_WEEK
                  FROM T_TEMP_DATES AS TTD
              ORDER BY RESULT_DATE ASC
          ) TTD
     LEFT JOIN (
                SELECT MR.TREATMENT_NAME
                      ,MR.WRITE_DATE
                      ,SUM(IFNULL(MP.TOTAL_AMOUNT, 0)) AS TOTAL_AMOUNT
                  FROM (
                        SELECT MR.MU_RESERVE_ID    AS MU_RESERVE_ID
                              ,MR.TREATMENT_NAME   AS TREATMENT_NAME
                              ,MR.WRITE_DATE       AS WRITE_DATE
                          FROM MU_RESERVE AS MR
                         WHERE MR.USE_YN = 'Y'
                           AND MR.CRM_MEMBER_SCHEDULE_PID IS NULL
                           AND MR.RESERVE_DATE &gt;= #{thisYearMonth}
                           AND MR.RESERVE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                           AND MR.STATUS IN ('Y')
                  ) AS MR
             LEFT JOIN (
                        SELECT MPD.MU_RESERVE_ID AS MU_RESERVE_ID
                              ,SUM(IFNULL(MPD.TOTAL_AMOUNT, 0))  AS TOTAL_AMOUNT
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MPD.MU_TREATMENT_ID != 'T172059821942700000000019'
                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                      GROUP BY MPD.MU_RESERVE_ID
                  ) AS MP
                    ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                 WHERE 1 = 1
                   AND MR.TREATMENT_NAME = #{treatmentName}
              GROUP BY MR.TREATMENT_NAME
                      ,MR.WRITE_DATE
     ) AS MP
            ON TTD.RESULT_DATE = MP.WRITE_DATE
        
        
        
        
        
<!--        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT #{thisYearMonth} AS RESULT_DATE

            UNION ALL

            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
              FROM T_TEMP_DATES
             WHERE RESULT_DATE &lt; LAST_DAY(#{thisYearMonth})
        )
        SELECT TTD.RESULT_DATE                                            AS "resultDate"
              ,CONCAT(TTD.MONTH, '.', TTD.DAY, '(', TTD.DAY_OF_WEEK, ')') AS "date"
              ,IFNULL(MP.TOTAL_AMOUNT, 0)                                 AS "totalAmount"
          FROM (
                SELECT TTD.RESULT_DATE                  AS RESULT_DATE
                      ,MONTH(TTD.RESULT_DATE)           AS MONTH
                      ,DAY(TTD.RESULT_DATE)             AS DAY
                      ,CASE DAYOFWEEK(TTD.RESULT_DATE)
                       WHEN 1 THEN '일'
                       WHEN 2 THEN '월'
                       WHEN 3 THEN '화'
                       WHEN 4 THEN '수'
                       WHEN 5 THEN '목'
                       WHEN 6 THEN '금'
                       WHEN 7 THEN '토'
                       END                           AS DAY_OF_WEEK
                  FROM T_TEMP_DATES AS TTD
              ORDER BY RESULT_DATE ASC
          ) TTD
     LEFT JOIN (
                SELECT MP.TREATMENT_NAME                                         AS TREATMENT_NAME
                      ,MP.WRITE_DATE                                             AS WRITE_DATE
                      ,SUM(MP.TOTAL_AMOUNT)  AS TOTAL_AMOUNT
                  FROM (
                        SELECT IF(MPD.TREATMENT_NAME = '멤버십', '기타', MPD.TREATMENT_NAME)           AS TREATMENT_NAME
                              ,MPD.TOTAL_AMOUNT                AS TOTAL_AMOUNT
                              ,MPD.MU_RESERVE_ID                                                     AS MU_RESERVE_ID
                              ,MPD.WRITE_DATE                                                        AS WRITE_DATE
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                    INNER JOIN (
                                SELECT MP.PAYMENT_NUMBER
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MP.PAYMENT_NUMBER
                          ) AS MP2
                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                  ) AS MP
            INNER JOIN MU_RESERVE AS MR
                    ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                   AND MR.USE_YN = 'Y'
                   AND MP.TREATMENT_NAME = #{treatmentName}
              GROUP BY MP.TREATMENT_NAME
                      ,MP.WRITE_DATE
     ) AS MP
            ON TTD.RESULT_DATE = MP.WRITE_DATE -->
    </select>

    <select id="selectListStatisticsClinicMember" parameterType="hashmap" resultType="hashmap">
        SELECT MM.MU_MEMBER_ID             AS "muMemberId"
              ,MM.MEMBER_NAME              AS "memberName"
              ,IFNULL(MP.TOTAL_COUNT, 0)   AS "totalCount"
          FROM (
                SELECT MM.MU_MEMBER_ID   AS MU_MEMBER_ID
                      ,NAME              AS MEMBER_NAME
                  FROM MU_MEMBER AS MM
                 WHERE MM.USE_YN = 'Y'
                   AND MM.TYPE IN ('D')
          ) AS MM
     LEFT JOIN (
                SELECT MP.RESERVE_MEMBER_ID  AS RESERVE_MEMBER_ID
                      ,COUNT(*)              AS TOTAL_COUNT
                  FROM (
                        SELECT MR.RESERVE_MEMBER_ID
                          FROM (
                                SELECT MR.MU_RESERVE_ID
                                      ,MR.RESERVE_MEMBER_ID
                                  FROM MU_RESERVE AS MR
                                 WHERE MR.USE_YN = 'Y'
                                   AND MR.CRM_MEMBER_SCHEDULE_PID IS NULL
                                   AND MR.RESERVE_DATE &gt;= #{thisYearMonth}
                                   AND MR.RESERVE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                   AND MR.STATUS IN ('Y')
                          ) AS MR
                     LEFT JOIN (
                                SELECT MPD.MU_RESERVE_ID AS MU_RESERVE_ID
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MPD.MU_RESERVE_ID
                          ) AS MP
                            ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                  ) AS MP
              GROUP BY MP.RESERVE_MEMBER_ID
          ) AS MP
            ON MM.MU_MEMBER_ID = MP.RESERVE_MEMBER_ID




<!--        SELECT MM.MU_MEMBER_ID             AS "muMemberId"
              ,MM.MEMBER_NAME              AS "memberName"
              ,IFNULL(MP.TOTAL_COUNT, 0)   AS "totalCount"
          FROM (
                SELECT MM.MU_MEMBER_ID   AS MU_MEMBER_ID
                      ,NAME              AS MEMBER_NAME
                  FROM MU_MEMBER AS MM
                 WHERE MM.USE_YN = 'Y'
                   AND MM.TYPE IN ('D')
          ) AS MM
     LEFT JOIN (
                SELECT MP.RESERVE_MEMBER_ID  AS RESERVE_MEMBER_ID
                      ,COUNT(*)              AS TOTAL_COUNT
                  FROM (
                        SELECT MPD.RESERVE_MEMBER_ID   AS RESERVE_MEMBER_ID
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                    INNER JOIN (
                                SELECT MP.PAYMENT_NUMBER
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MP.PAYMENT_NUMBER
                          ) AS MP2
                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                  ) AS MP
              GROUP BY MP.RESERVE_MEMBER_ID
          ) AS MP
            ON MM.MU_MEMBER_ID = MP.RESERVE_MEMBER_ID -->
    </select>

    <select id="selectListStatisticsPaymentTreatmentTop" parameterType="hashmap" resultType="hashmap">
        SELECT MP.TREATMENT_NAME             AS "treatmentName"
              ,MP.TREATMENT_PROCEDURE_NAME   AS "treatmentProcedureName"
              ,MP.TOTAL_AMOUNT               AS "totalAmount"
              ,MP.TOTAL_COUNT                AS "totalCount"
          FROM (
                SELECT MP.TREATMENT_NAME                                         AS TREATMENT_NAME
                      ,MP.TREATMENT_PROCEDURE_NAME                               AS TREATMENT_PROCEDURE_NAME
                      ,SUM(MP.TOTAL_AMOUNT)                                      AS TOTAL_AMOUNT
                      ,SUM(MP.TOTAL_COUNT)                                       AS TOTAL_COUNT
                  FROM (
                        SELECT MR.TREATMENT_NAME
                              ,MR.TREATMENT_PROCEDURE_NAME
                              ,SUM(IFNULL(MP.TOTAL_AMOUNT, 0)) AS TOTAL_AMOUNT
                              ,COUNT(*)                        AS TOTAL_COUNT
                          FROM (
                                SELECT MR.MU_RESERVE_ID    AS MU_RESERVE_ID
                                      ,MR.TREATMENT_NAME   AS TREATMENT_NAME
                                      ,MR.TREATMENT_PROCEDURE_NAME   AS TREATMENT_PROCEDURE_NAME
                                  FROM MU_RESERVE AS MR
                                 WHERE MR.USE_YN = 'Y'
                                   AND MR.CRM_MEMBER_SCHEDULE_PID IS NULL
                                   AND MR.RESERVE_DATE &gt;= #{thisYearMonth}
                                   AND MR.RESERVE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                   AND MR.STATUS IN ('Y')
                          ) AS MR
                     LEFT JOIN (
                                SELECT MPD.MU_RESERVE_ID AS MU_RESERVE_ID
                                      ,SUM(IFNULL(MPD.TOTAL_AMOUNT, 0))  AS TOTAL_AMOUNT
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MPD.MU_TREATMENT_ID != 'T172059821942700000000019'
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MPD.MU_RESERVE_ID
                          ) AS MP
                            ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                      GROUP BY MR.TREATMENT_NAME
                              ,MR.TREATMENT_PROCEDURE_NAME
                  ) AS MP
              GROUP BY MP.TREATMENT_NAME
                      ,MP.TREATMENT_PROCEDURE_NAME
          ) AS MP
         WHERE 1 = 1
      ORDER BY MP.TOTAL_AMOUNT DESC
         LIMIT 0, 10



<!--        SELECT MP.TREATMENT_NAME             AS "treatmentName"
              ,MP.TREATMENT_PROCEDURE_NAME   AS "treatmentProcedureName"
              ,MP.TOTAL_AMOUNT               AS "totalAmount"
              ,MP.TOTAL_COUNT                AS "totalCount"
          FROM (
                SELECT MP.TREATMENT_NAME                                         AS TREATMENT_NAME
                      ,MP.TREATMENT_PROCEDURE_NAME                               AS TREATMENT_PROCEDURE_NAME
                      ,SUM(MP.TOTAL_AMOUNT)                                      AS TOTAL_AMOUNT
                      ,COUNT(CASE
                             WHEN MP.STATUS IN ('Y', 'R', 'NP', 'NY', 'P', 'PY')
                             THEN 1
                             END)                                                AS TOTAL_COUNT
                  FROM (
                        SELECT IF(MPD.TREATMENT_NAME = '멤버십', '기타', MPD.TREATMENT_NAME) AS TREATMENT_NAME
                              ,MPD.TREATMENT_PROCEDURE_NAME                                AS TREATMENT_PROCEDURE_NAME
                              ,MPD.TOTAL_AMOUNT                                            AS TOTAL_AMOUNT
                              ,MP.STATUS                                                   AS STATUS
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                           AND MPD.MU_TREATMENT_ID != 'T172059821942700000000019'
                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                    INNER JOIN (
                                SELECT MP.PAYMENT_NUMBER
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MPD.MU_TREATMENT_ID != 'T172059821942700000000019'
                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                   AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MP.PAYMENT_NUMBER
                          ) AS MP2
                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                  ) AS MP
              GROUP BY MP.TREATMENT_NAME
                      ,MP.TREATMENT_PROCEDURE_NAME
          ) AS MP
         WHERE 1 = 1
      ORDER BY MP.TOTAL_AMOUNT DESC -->
    </select>

    <select id="selectListStatisticsPaymentClinic" parameterType="hashmap" resultType="hashmap">
        SELECT *
          FROM (
                SELECT MP.PAYMENT_NUMBER                                                  AS "paymentNumber"
                      ,MP.MAX_RESERVE_DATE                                                AS "reserveDate"
                      ,MP.MAX_PAYMENT_DATE                                                AS "paymentDate"
                      ,IF(MP.PAYMENT_CATEGORY_ITEM_NAME = ''
                         ,IF(DISCOUNT_PRICE = MP.ORG_TREATMENT_PROCEDURE_PRICE+MP.ORG_TREATMENT_PROCEDURE_VAT
                            ,'-'
                            ,'미납')
                         ,REGEXP_REPLACE(REGEXP_REPLACE(MP.PAYMENT_CATEGORY_ITEM_NAME
                                                        ,'([^,]+)(,\\1)+'
                                                        ,'\\1')
                                        ,',+'
                                        ,','))                                            AS "paymentCategoryItemName"
                      ,MP.TREATMENT_NAME                                                  AS "treatmentName"
                      ,MP.TREATMENT_PROCEDURE_NAME                                        AS "treatmentProcedureNm"
                      ,MP.RESERVE_MEMBER_NAME                                             AS "reserveMemberName"
                      ,MP.MU_USER_ID                                                      AS "muUserId"
                      ,MP.USER_NAME                                                       AS "userName"
                      ,MP.MEMBER_NAME                                                     AS "muMemberName"
                      ,MP.ETC                                                             AS "etc"
                      ,MP.ORG_TREATMENT_PROCEDURE_PRICE+MP.ORG_TREATMENT_PROCEDURE_VAT    AS "totalAmount"
                      ,MP.DISCOUNT_PRICE                                                  AS "discountAmount"
                      ,MP.MEMBERSHIP_PRICE + MP.REFUND_MEMBERSHIP_PRICE                   AS "membershipPrice"
                      ,IF(MP.STATUS IN ('NP', 'P', 'NY', 'PY')
                         ,MP.ORG_TREATMENT_PROCEDURE_PRICE+MP.ORG_TREATMENT_PROCEDURE_VAT - MP.PAY_AMOUNT + MP.DISCOUNT_PRICE - MP.MEMBERSHIP_PRICE
                         ,0)                                                              AS "nonpaymentAmount"
                      ,MP.PAY_AMOUNT + MP.REFUND_TOTAL_AMOUNT                             AS "paymentAmount"
                      ,MP.AMOUNT                                                          AS "amount"
                      ,MP.VAT                                                             AS "vat"
                      ,MP.REFUND_TOTAL_AMOUNT                                             AS "refundAmount"
                      ,CASE
                       WHEN MP.STATUS = 'C'  THEN '결제취소'
                       WHEN MP.STATUS = 'R'  THEN '환불'
                       WHEN MP.STATUS = 'PY' THEN '부분결제(완료)'
                       WHEN MP.STATUS = 'NY' THEN '미수결제'
                       WHEN MP.STATUS = 'CN' THEN '재결제(취소)'
                       WHEN MP.STATUS = 'CY' THEN '재결제(완료)'
                       WHEN MP.STATUS = 'Y'  THEN '결제완료'
                       WHEN MP.STATUS = 'P'  THEN '부분결제'
                       WHEN MP.STATUS = 'NP' THEN '미수'
                       ELSE ''
                        END                                                               AS "status1"
                      ,MP.MAX_REG_DATE                                                    AS "regDate"
                      ,CASE
                       WHEN RESERVE_STATUS = 'T' THEN '예약'
                       WHEN RESERVE_STATUS = 'TN' THEN '예약취소'
                       WHEN RESERVE_STATUS = 'R' THEN '접수'
                       WHEN RESERVE_STATUS = 'RN' THEN '접수취소'
                       WHEN RESERVE_STATUS = 'G' THEN '진료중'
                       WHEN RESERVE_STATUS = 'Y' THEN '진료완료'
                       ELSE ''
                       END                                                                AS "reserveStatus"
                  FROM (
                        SELECT MP.PAYMENT_NUMBER                                                       AS PAYMENT_NUMBER
                              ,GROUP_CONCAT(DISTINCT MP.MAX_RESERVE_DATE)                              AS MAX_RESERVE_DATE
                              ,GROUP_CONCAT(DISTINCT MP.MAX_PAYMENT_DATE)                              AS MAX_PAYMENT_DATE
                              ,GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME)                    AS PAYMENT_CATEGORY_ITEM_NAME
                              ,GROUP_CONCAT(DISTINCT MP.MU_TREATMENT_ID)                               AS MU_TREATMENT_ID
                              ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                                AS TREATMENT_NAME
                              ,GROUP_CONCAT(MP.MU_TREATMENT_PROCEDURE_ID)                              AS MU_TREATMENT_PROCEDURE_ID
                              ,GROUP_CONCAT(MP.TREATMENT_PROCEDURE_NAME)                               AS TREATMENT_PROCEDURE_NAME
                              ,GROUP_CONCAT(DISTINCT MP.RESERVE_MEMBER_NAME)                           AS RESERVE_MEMBER_NAME
                              ,GROUP_CONCAT(DISTINCT MP.MU_USER_ID)                                    AS MU_USER_ID
                              ,GROUP_CONCAT(DISTINCT MP.USER_NAME)                                     AS USER_NAME
                              ,GROUP_CONCAT(DISTINCT MP.MEMBER_NAME)                                   AS MEMBER_NAME
                              ,GROUP_CONCAT(DISTINCT MP.ETC)                                           AS ETC
                              ,SUM(MP.PAY_AMOUNT)                                                      AS PAY_AMOUNT
                              ,SUM(MP.DISCOUNT_PRICE)                                                  AS DISCOUNT_PRICE
                              ,SUM(MP.MEMBERSHIP_PRICE)                                                AS MEMBERSHIP_PRICE
                              ,SUM(MP.AMOUNT)                                                          AS AMOUNT
                              ,SUM(MP.VAT)                                                             AS VAT
                              ,SUM(MP.REFUND_TOTAL_AMOUNT)                                             AS REFUND_TOTAL_AMOUNT
                              ,SUM(MP.REFUND_MEMBERSHIP_PRICE)                                         AS REFUND_MEMBERSHIP_PRICE
                              ,IFNULL((SELECT MP2.STATUS
                                         FROM MU_PAYMENT AS MP2
                                        WHERE MP2.USE_YN = 'Y'
                                          AND MP2.PAYMENT_NUMBER = MP.PAYMENT_NUMBER
                                       <if test="paymentSearchStartDate != null and paymentSearchStartDate != '' and paymentSearchEndDate != null and paymentSearchEndDate != ''">
                                          AND MP2.WRITE_DATE &gt;= #{paymentSearchStartDate}
                                          AND MP2.WRITE_DATE &lt;= #{paymentSearchEndDate}
                                       </if>
                                       <if test="muUserId != null and muUserId != ''">
                                          AND MP.MU_USER_ID = #{muUserId}
                                       </if>
                                     ORDER BY MP2.REG_DATE DESC
                                        LIMIT 0, 1), '')                                               AS STATUS
                              ,GROUP_CONCAT(DISTINCT MP.MAX_REG_DATE)                                  AS MAX_REG_DATE
                              ,SUM((SELECT MTPP.PRICE
                                      FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                     WHERE MTPP.USE_YN = 'Y'
                                       AND MTPP.MU_TREATMENT_PROCEDURE_ID = MP.MU_TREATMENT_PROCEDURE_ID
                                     LIMIT 0, 1))                                                      AS ORG_TREATMENT_PROCEDURE_PRICE
                              ,SUM((SELECT MTPP.VAT
                                      FROM MU_TREATMENT_PROCEDURE_PRICE AS MTPP
                                     WHERE MTPP.USE_YN = 'Y'
                                       AND MTPP.MU_TREATMENT_PROCEDURE_ID = MP.MU_TREATMENT_PROCEDURE_ID
                                     LIMIT 0, 1))                                                      AS ORG_TREATMENT_PROCEDURE_VAT
                              ,GROUP_CONCAT(DISTINCT MP.RESERVE_STATUS)                                AS RESERVE_STATUS
                          FROM (
                                SELECT MP.PAYMENT_NUMBER                                                 AS PAYMENT_NUMBER
                                      ,MP.MU_USER_ID                                                     AS MU_USER_ID
                                      ,MP.USER_NAME                                                      AS USER_NAME
                                      ,MAX(MP.RESERVE_DATE)                                              AS MAX_RESERVE_DATE
                                      ,MAX(MP.PAYMENT_DATE)                                              AS MAX_PAYMENT_DATE
                                      ,GROUP_CONCAT(DISTINCT MP.PAYMENT_CATEGORY_ITEM_NAME)              AS PAYMENT_CATEGORY_ITEM_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.MU_TREATMENT_ID)                         AS MU_TREATMENT_ID
                                      ,GROUP_CONCAT(DISTINCT MP.TREATMENT_NAME)                          AS TREATMENT_NAME
                                      ,MP.MU_TREATMENT_PROCEDURE_ID                                      AS MU_TREATMENT_PROCEDURE_ID
                                      ,GROUP_CONCAT(DISTINCT MP.TREATMENT_PROCEDURE_NAME SEPARATOR ', ') AS TREATMENT_PROCEDURE_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.RESERVE_MEMBER_NAME SEPARATOR ', ')      AS RESERVE_MEMBER_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.MEMBER_NAME SEPARATOR ', ')              AS MEMBER_NAME
                                      ,GROUP_CONCAT(DISTINCT MP.ETC)                                     AS ETC
                                      ,SUM(MP.PAY_AMOUNT)                                                AS PAY_AMOUNT
                                      ,SUM(MP.DISCOUNT_PRICE)                                            AS DISCOUNT_PRICE
                                      ,SUM(MP.MEMBERSHIP_PRICE)                                          AS MEMBERSHIP_PRICE
                                      ,SUM(MP.AMOUNT)                                                    AS AMOUNT
                                      ,SUM(MP.VAT)                                                       AS VAT
                                      ,SUM(MP.REFUND_TOTAL_AMOUNT)                                       AS REFUND_TOTAL_AMOUNT
                                      ,SUM(MP.REFUND_MEMBERSHIP_PRICE)                                   AS REFUND_MEMBERSHIP_PRICE
                                      ,MAX(MP.REG_DATE)                                                  AS MAX_REG_DATE
                                      ,GROUP_CONCAT(DISTINCT MP.RESERVE_STATUS)                          AS RESERVE_STATUS
                                  FROM (
                                        SELECT MP.RESERVE_DATE                                                       AS RESERVE_DATE
                                              ,MP.PAYMENT_DATE                                                       AS PAYMENT_DATE
                                              ,IF(MP.STATUS IN ('NP')
                                                 ,NULL
                                                 ,MP.PAYMENT_CATEGORY_ITEM_NAME)                                     AS PAYMENT_CATEGORY_ITEM_NAME
                                              ,MP.MU_USER_ID                                                         AS MU_USER_ID
                                              ,MP.USER_NAME                                                          AS USER_NAME
                                              ,MP.MU_TREATMENT_ID                                                    AS MU_TREATMENT_ID
                                              ,MP.TREATMENT_NAME                                                     AS TREATMENT_NAME
                                              ,MP.MU_TREATMENT_PROCEDURE_ID                                          AS MU_TREATMENT_PROCEDURE_ID
                                              ,MP.TREATMENT_PROCEDURE_NAME                                           AS TREATMENT_PROCEDURE_NAME
                                              ,MP.RESERVE_MEMBER_ID                                                  AS RESERVE_MEMBER_ID
                                              ,MP.RESERVE_MEMBER_NAME                                                AS RESERVE_MEMBER_NAME
                                              ,MP.MU_MEMBER_ID                                                       AS MU_MEMBER_ID
                                              ,MP.MEMBER_NAME                                                        AS MEMBER_NAME
                                              ,NULLIF(MP.ETC, '')                                                    AS ETC
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, MP.PAY_AMOUNT)                   AS PAY_AMOUNT
                                              ,MP.DISCOUNT_PRICE                                                     AS DISCOUNT_PRICE
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), 0, IFNULL(MP.MEMBERSHIP_PRICE, 0))  AS MEMBERSHIP_PRICE
                                              ,IF(MP.STATUS IN ('R', 'C'), 0, MP.AMOUNT)                             AS AMOUNT
                                              ,IF(MP.STATUS IN ('R', 'C'), 0, MP.VAT)                                AS VAT
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), MP.PAY_AMOUNT, 0)                   AS REFUND_TOTAL_AMOUNT
                                              ,IF(MP.STATUS IN ('R', 'C', 'CN'), IFNULL(MP.MEMBERSHIP_PRICE, 0), 0)  AS REFUND_MEMBERSHIP_PRICE
                                              ,MP.PAYMENT_NUMBER                                                     AS PAYMENT_NUMBER
                                              ,MP.STATUS                                                             AS STATUS
                                              ,MP.REG_DATE                                                           AS REG_DATE
                                              ,MP.MU_RESERVE_ID                                                      AS MU_RESERVE_ID
                                              ,MP.RESERVE_STATUS                                                     AS RESERVE_STATUS
                                          FROM (
                                                SELECT IFNULL(DATE_FORMAT(MR.RESERVE_DATE, '%Y-%m-%d'), '-')   AS RESERVE_DATE
                                                      ,DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')                  AS PAYMENT_DATE
                                                      ,MP.PAYMENT_CATEGORY_ITEM_NAME                           AS PAYMENT_CATEGORY_ITEM_NAME
                                                      ,MP.MU_USER_ID                                           AS MU_USER_ID
                                                      ,(SELECT MU.USER_NAME
                                                          FROM MU_USER AS MU
                                                         WHERE MU.USE_YN = 'Y'
                                                           AND MU.MU_USER_ID = MP.MU_USER_ID
                                                         LIMIT 0, 1)                                           AS USER_NAME
                                                      ,MPD.MU_TREATMENT_ID                                     AS MU_TREATMENT_ID
                                                      ,MPD.TREATMENT_NAME                                      AS TREATMENT_NAME
                                                      ,MPD.MU_TREATMENT_PROCEDURE_ID                           AS MU_TREATMENT_PROCEDURE_ID
                                                      ,MPD.TREATMENT_PROCEDURE_NAME                            AS TREATMENT_PROCEDURE_NAME
                                                      ,MPD.RESERVE_MEMBER_ID                                   AS RESERVE_MEMBER_ID
                                                      ,(SELECT MM.NAME
                                                          FROM MU_MEMBER AS MM
                                                         WHERE MM.USE_YN = 'Y'
                                                           AND MM.MU_MEMBER_ID = MPD.RESERVE_MEMBER_ID
                                                         LIMIT 0, 1)                                           AS RESERVE_MEMBER_NAME
                                                      ,MPD.MU_MEMBER_ID                                        AS MU_MEMBER_ID
                                                      ,(SELECT MM.NAME
                                                          FROM MU_MEMBER AS MM
                                                         WHERE MM.USE_YN = 'Y'
                                                           AND MM.MU_MEMBER_ID = MPD.MU_MEMBER_ID
                                                         LIMIT 0, 1)                                           AS MEMBER_NAME
                                                      ,MP.ETC                                    AS ETC
                                                      ,MPD.TOTAL_AMOUNT                          AS PAY_AMOUNT
                                                      ,MPD.DISCOUNT_PRICE                        AS DISCOUNT_PRICE
                                                      ,MPD.MEMBERSHIP_PRICE                      AS MEMBERSHIP_PRICE
                                                      ,MPD.AMOUNT                                AS AMOUNT
                                                      ,MPD.VAT                                   AS VAT
                                                      ,MP.PAYMENT_NUMBER                         AS PAYMENT_NUMBER
                                                      ,MP.STATUS                                 AS STATUS
                                                      ,MP.REG_DATE                               AS REG_DATE
                                                      ,MPD.MU_RESERVE_ID                         AS MU_RESERVE_ID
                                                      ,MR.STATUS                                 AS RESERVE_STATUS
                                                  FROM MU_PAYMENT AS MP
                                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                   AND MP.USE_YN = 'Y'
                                                   AND MPD.USE_YN = 'Y'
                                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                   AND MPD.MU_TREATMENT_ID != 'T172059821942700000000019'
                                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                            INNER JOIN (
                                                        SELECT MP.PAYMENT_NUMBER
                                                          FROM MU_PAYMENT AS MP
                                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                           AND MP.USE_YN = 'Y'
                                                           AND MPD.USE_YN = 'Y'
                                                           AND MPD.MU_TREATMENT_ID != 'T172059821942700000000019'
                                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                                      GROUP BY MP.PAYMENT_NUMBER
                                                  ) AS MP2
                                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                             LEFT JOIN MU_RESERVE AS MR
                                                    ON MPD.MU_RESERVE_ID = MR.MU_RESERVE_ID
                                          ) AS MP
                                         WHERE 1 = 1
                                  ) AS MP
                                 WHERE 1 = 1
                              GROUP BY MP.PAYMENT_NUMBER
                                      ,MP.MU_USER_ID
                                      ,MP.USER_NAME
                                      ,MP.MU_RESERVE_ID
                                      ,MP.MU_TREATMENT_PROCEDURE_ID
                          ) AS MP
                         WHERE 1 = 1
                      GROUP BY MP.PAYMENT_NUMBER
                  ) AS MP
                 WHERE 1 = 1
          ) AS MP
         WHERE 1 = 1
        <choose>
        <when test="sort != null and sort != ''">
      ORDER BY ${sort}
        </when>
        <otherwise>
      ORDER BY paymentDate DESC
              ,regDate DESC
        </otherwise>
        </choose>
    </select>

    <select id="selectStatisticsVisitorFirstYn" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(CASE WHEN MP.FIRST_VISIT_YN = 'Y' THEN 1 END)   AS "totalCountY"
              ,COUNT(CASE WHEN MP.FIRST_VISIT_YN = 'N' THEN 1 END)   AS "totalCountN"
          FROM (
                SELECT MP.WRITE_DATE
                      ,MP.MU_USER_ID
                      ,MP.FIRST_VISIT_YN
                  FROM MU_PAYMENT AS MP
                 WHERE MP.USE_YN = 'Y'
                   AND MP.STATUS != 'C'
                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
              GROUP BY MP.WRITE_DATE
                      ,MP.MU_USER_ID
                      ,MP.FIRST_VISIT_YN
          ) AS MP
    </select>

    <select id="selectTotalVisitorMembership" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(CASE WHEN MU.MEMBERSHIP_YN = 'Y' THEN 1 END) AS "totalCountY"
              ,COUNT(CASE WHEN MU.MEMBERSHIP_YN = 'N' THEN 1 END) AS "totalCountN"
          FROM (
                SELECT MP.WRITE_DATE
                      ,MP.MU_USER_ID
                  FROM MU_PAYMENT AS MP
                 WHERE MP.USE_YN = 'Y'
                   AND MP.STATUS != 'C'
                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
              GROUP BY MP.WRITE_DATE
                      ,MP.MU_USER_ID
          ) AS MP
    INNER JOIN MU_USER AS MU
            ON MP.MU_USER_ID = MU.MU_USER_ID
           AND MU.USE_YN = 'Y'
    </select>

    <select id="selectStatisticsThisVisitorFirstYn" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT #{thisYearMonth} AS RESULT_DATE

            UNION ALL

            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
              FROM T_TEMP_DATES
             WHERE RESULT_DATE &lt; LAST_DAY(#{thisYearMonth})
        )
        SELECT TTD.RESULT_DATE                                            AS "resultDate"
              ,CONCAT(TTD.MONTH, '.', TTD.DAY, '(', TTD.DAY_OF_WEEK, ')') AS "date"
              ,IFNULL(MP.TOTAL_COUNT, 0)                                  AS "totalCount"
          FROM (
                SELECT TTD.RESULT_DATE                  AS RESULT_DATE
                      ,MONTH(TTD.RESULT_DATE)           AS MONTH
                      ,DAY(TTD.RESULT_DATE)             AS DAY
                      ,CASE DAYOFWEEK(TTD.RESULT_DATE)
                       WHEN 1 THEN '일'
                       WHEN 2 THEN '월'
                       WHEN 3 THEN '화'
                       WHEN 4 THEN '수'
                       WHEN 5 THEN '목'
                       WHEN 6 THEN '금'
                       WHEN 7 THEN '토'
                       END                           AS DAY_OF_WEEK
                  FROM T_TEMP_DATES AS TTD
              ORDER BY RESULT_DATE ASC
          ) TTD
     LEFT JOIN (
                SELECT MP.WRITE_DATE  AS WRITE_DATE
                      ,COUNT(*)       AS TOTAL_COUNT
                  FROM (
                        SELECT MP.WRITE_DATE   AS WRITE_DATE
                              ,MP.MU_USER_ID   AS MU_USER_ID
                          FROM MU_PAYMENT AS MP
                         WHERE MP.USE_YN = 'Y'
                           AND MP.STATUS != 'C'
                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                           AND MP.FIRST_VISIT_YN = #{firstYn}
                      GROUP BY MP.WRITE_DATE
                              ,MP.MU_USER_ID
                  ) AS MP
              GROUP BY MP.WRITE_DATE
     ) AS MP
            ON TTD.RESULT_DATE = MP.WRITE_DATE
    </select>

    <select id="selectStatisticsVisitorCategory" parameterType="hashmap" resultType="hashmap">
        SELECT MCI.CATEGORY_ITEM_NAME      AS "categoryItem"
              ,IFNULL(MP.TOTAL_COUNT, 0)   AS "totalCount"
          FROM (
                SELECT MCI.CATEGORY_ITEM_NAME   AS CATEGORY_ITEM_NAME
                  FROM MU_CATEGORY_ITEM AS MCI
                 WHERE MCI.USE_YN = 'Y'
                   AND MCI.MU_CATEGORY_ID = 'C17128016604480000000004'

                UNION ALL

                SELECT '기타'   AS CATEGORY_ITEM_NAME
          ) AS MCI
     LEFT JOIN (
                SELECT MP.RESERVE_CATEGORY_ITEM_NAME   AS RESERVE_CATEGORY_ITEM_NAME
                      ,COUNT(*)                        AS TOTAL_COUNT
                  FROM (
                        SELECT IFNULL((SELECT MR.RESERVE_CATEGORY_ITEM_NAME
                                         FROM MU_RESERVE AS MR
                                        WHERE MR.USE_YN = 'Y'
                                          AND MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                                        LIMIT 0, 1), MP.MU_RESERVE_ID)                 AS RESERVE_CATEGORY_ITEM_NAME
                          FROM (
                                SELECT CASE
                                       WHEN MPD.MU_RESERVE_ID IS NOT NULL
                                       THEN MPD.MU_RESERVE_ID
                                       ELSE '기타'
                                       END                                   AS MU_RESERVE_ID
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MP.STATUS != 'C'
                                   AND MPD.MU_RESERVE_ID IS NOT NULL
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                          ) AS MP
                  ) AS MP
              GROUP BY MP.RESERVE_CATEGORY_ITEM_NAME
     ) AS MP
        ON MCI.CATEGORY_ITEM_NAME = MP.RESERVE_CATEGORY_ITEM_NAME
    </select>

    <select id="selectStatisticsVisitorGenderAge" parameterType="hashmap" resultType="hashmap">
        SELECT MP.AGE    AS "age"
              ,COUNT(*)  AS "count"
          FROM (
                SELECT MU.GENDER
                      ,YEAR(NOW()) - YEAR(MU.BIRTHDAY) AS AGE
                  FROM (
                        SELECT MP.WRITE_DATE
                              ,MP.MU_USER_ID
                          FROM MU_PAYMENT AS MP
                         WHERE MP.USE_YN = 'Y'
                           AND MP.STATUS != 'C'
                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                      GROUP BY MP.WRITE_DATE
                              ,MP.MU_USER_ID
                  ) AS MP
            INNER JOIN MU_USER AS MU
                    ON MP.MU_USER_ID = MU.MU_USER_ID
                   AND MU.USE_YN = 'Y'
                   AND MU.GENDER = #{gender}
          ) AS MP
      GROUP BY MP.AGE
    </select>

    <select id="selectStatisticsVisitorNationality" parameterType="hashmap" resultType="hashmap">
        SELECT MP.CATEGORY_ITEM_NAME    AS "categoryItemName"
              ,MP.TOTAL_COUNT           AS "totalCount"
              ,MP.TOTAL_AMOUNT          AS "totalAmount"
          FROM (
                SELECT MCI.CATEGORY_ITEM_NAME        AS CATEGORY_ITEM_NAME
                      ,IFNULL(MP.TOTAL_COUNT, 0)     AS TOTAL_COUNT
                      ,IFNULL(MP.TOTAL_AMOUNT, 0)    AS TOTAL_AMOUNT
                  FROM (
                        SELECT MCI.MU_CATEGORY_ITEM_ID    AS MU_CATEGORY_ITEM_ID
                              ,MCI.CATEGORY_ITEM_NAME     AS CATEGORY_ITEM_NAME
                          FROM MU_CATEGORY_ITEM AS MCI
                         WHERE MCI.USE_YN = 'Y'
                           AND MCI.MU_CATEGORY_ID = 'C17128016604480000000001'
                  ) AS MCI
             LEFT JOIN (
                        SELECT MP.NATIONALITY    AS NATIONALITY
                              ,COUNT(*)          AS TOTAL_COUNT
                              ,SUM(TOTAL_AMOUNT) AS TOTAL_AMOUNT
                          FROM (
                                SELECT MP.MU_USER_ID    AS MU_USER_ID
                                      ,MU.NATIONALITY   AS NATIONALITY
                                      ,MP.TOTAL_AMOUNT  AS TOTAL_AMOUNT
                                  FROM (
                                        SELECT MP.MU_USER_ID    AS MU_USER_ID
                                              ,MPD.TOTAL_AMOUNT AS TOTAL_AMOUNT
                                          FROM MU_PAYMENT AS MP
                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                           AND MP.USE_YN = 'Y'
                                           AND MPD.USE_YN = 'Y'
                                           AND MPD.MU_RESERVE_ID IS NOT NULL
                                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                    INNER JOIN (
                                                SELECT MP.PAYMENT_NUMBER
                                                  FROM MU_PAYMENT AS MP
                                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                                   AND MP.USE_YN = 'Y'
                                                   AND MPD.USE_YN = 'Y'
                                                   AND MP.CRM_MEMBER_PAY_PID IS NULL
                                                   AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                              GROUP BY MP.PAYMENT_NUMBER
                                          ) AS MP2
                                            ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                                  ) AS MP
                            INNER JOIN MU_USER AS MU
                                    ON MU.MU_USER_ID = MP.MU_USER_ID
                                   AND MU.USE_YN = 'Y'
                          ) AS MP
                      GROUP BY MP.NATIONALITY
             ) AS MP
                    ON MCI.MU_CATEGORY_ITEM_ID = MP.NATIONALITY
          ) AS MP
      ORDER BY MP.TOTAL_AMOUNT DESC
              ,MP.CATEGORY_ITEM_NAME ASC
    </select>

    <select id="selectStatisticsVisitorUserType" parameterType="hashmap" resultType="hashmap">
        SELECT MP.USER_TYPE      AS "categoryItemName"
              ,MP.TOTAL_COUNT    AS "totalCount"
              ,MP.TOTAL_AMOUNT   AS "totalAmount"
          FROM (
                SELECT MP.USER_TYPE      AS USER_TYPE
                      ,COUNT(*)          AS TOTAL_COUNT
                      ,SUM(TOTAL_AMOUNT) AS TOTAL_AMOUNT
                  FROM (
                        SELECT MP.MU_USER_ID                     AS MU_USER_ID
                              ,IFNULL((SELECT MCI.CATEGORY_ITEM_NAME
                                         FROM MU_CATEGORY_ITEM AS MCI
                                        WHERE MCI.USE_YN = 'Y'
                                          AND MCI.MU_CATEGORY_ITEM_ID = MU.USER_TYPE
                                        LIMIT 0, 1), '없음')     AS USER_TYPE
                              ,MP.TOTAL_AMOUNT                   AS TOTAL_AMOUNT
                          FROM (
                                SELECT MP.MU_USER_ID        AS MU_USER_ID
                                      ,MPD.TOTAL_AMOUNT     AS TOTAL_AMOUNT
                                  FROM MU_PAYMENT AS MP
                            INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                    ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                   AND MP.USE_YN = 'Y'
                                   AND MPD.USE_YN = 'Y'
                                   AND MPD.MU_RESERVE_ID IS NOT NULL
                                   AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                            INNER JOIN (
                                        SELECT MP.PAYMENT_NUMBER
                                          FROM MU_PAYMENT AS MP
                                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                                           AND MP.USE_YN = 'Y'
                                           AND MPD.USE_YN = 'Y'
                                           AND MP.CRM_MEMBER_PAY_PID IS NULL
                                           AND MPD.CRM_MEMBER_PAY_PID IS NULL
                                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                                      GROUP BY MP.PAYMENT_NUMBER
                                  ) AS MP2
                                    ON MP.PAYMENT_NUMBER = MP2.PAYMENT_NUMBER
                          ) AS MP
                    INNER JOIN MU_USER AS MU
                            ON MU.MU_USER_ID = MP.MU_USER_ID
                           AND MU.USE_YN = 'Y'
                  ) AS MP
              GROUP BY MP.USER_TYPE
          ) AS MP
      ORDER BY MP.TOTAL_AMOUNT DESC
              ,MP.USER_TYPE ASC
    </select>

    <select id="selectListStatisticsUser" parameterType="hashmap" resultType="hashmap">
        SELECT DATE_FORMAT(MP.WRITE_DATE, '%Y-%m-%d')    AS "writeDate"
              ,CASE DAYOFWEEK(MP.WRITE_DATE)
               WHEN 1 THEN '일'
               WHEN 2 THEN '월'
               WHEN 3 THEN '화'
               WHEN 4 THEN '수'
               WHEN 5 THEN '목'
               WHEN 6 THEN '금'
               WHEN 7 THEN '토'
               END                                       AS "dayOfWeek"
              ,CASE
               WHEN MP.FIRST_VISIT_YN = 'Y'
               THEN '초진'
               WHEN MP.FIRST_VISIT_YN = 'N'
               THEN '재진'
               ELSE ''
               END                                       AS "firstYn"
              ,MP.RESERVE_CATEGORY_ITEM_NAME             AS "reserveCategoryItemName"
              ,MP.TREATMENT_NAME                         AS "treatmentName"
              ,MP.TREATMENT_PROCEDURE_NAME               AS "treatmentProcedureName"
              ,MU.USER_NAME                              AS "userName"
              ,(SELECT MCI.CATEGORY_ITEM_NAME
                  FROM MU_CATEGORY_ITEM AS MCI
                 WHERE MCI.USE_YN = 'Y'
                   AND MCI.MU_CATEGORY_ITEM_ID = MU.NATIONALITY
                 LIMIT 0, 1)                             AS "nationalityName"
              ,DATE_FORMAT(MU.BIRTHDAY, '%Y-%m-%d')      AS "birthDay"
              ,CASE
               WHEN MU.GENDER = 'F'
               THEN '여성'
               WHEN MU.GENDER = 'M'
               THEN '남성'
               ELSE ''
               END                                       AS "gender"
          FROM (
                SELECT MP.TREATMENT_NAME                                    AS TREATMENT_NAME
                      ,MP.WRITE_DATE                                        AS WRITE_DATE
                      ,MP.AMOUNT - MP.DISCOUNT_PRICE - MP.MEMBERSHIP_PRICE  AS TOTAL_AMOUNT
                      ,MR.MU_USER_ID                                        AS MU_USER_ID
                      ,MP.FIRST_VISIT_YN                                    AS FIRST_VISIT_YN
                      ,MR.RESERVE_CATEGORY_ITEM_NAME                        AS RESERVE_CATEGORY_ITEM_NAME
                      ,MR.TREATMENT_PROCEDURE_NAME                          AS TREATMENT_PROCEDURE_NAME
                  FROM (
                        SELECT MPD.TREATMENT_NAME                         AS TREATMENT_NAME
                              ,CASE
                               WHEN MP.STATUS = 'NP' OR MP.STATUS = 'C'
                               THEN 0
                               WHEN MP.STATUS = 'R'
                               THEN MP.PAYMENT_AMOUNT - MP.REFUND_AMOUNT
                               ELSE MPD.TOTAL_AMOUNT
                               END                                        AS AMOUNT
                              ,CASE
                               WHEN MP.STATUS = 'NP' OR MP.STATUS = 'C'
                               THEN 0
                               WHEN MP.STATUS = 'R'
                               THEN MP.PAYMENT_AMOUNT - MP.REFUND_AMOUNT
                               ELSE MPD.DISCOUNT_PRICE
                               END                                        AS DISCOUNT_PRICE
                              ,CASE
                               WHEN MP.STATUS = 'NP' OR MP.STATUS = 'C'
                               THEN 0
                               WHEN MP.STATUS = 'R'
                               THEN MP.PAYMENT_AMOUNT - MP.REFUND_AMOUNT
                               ELSE IFNULL(MPD.MEMBERSHIP_PRICE, 0)
                               END                                        AS MEMBERSHIP_PRICE
                              ,MPD.MU_RESERVE_ID                          AS MU_RESERVE_ID
                              ,MPD.WRITE_DATE                             AS WRITE_DATE
                              ,MP.FIRST_VISIT_YN                          AS FIRST_VISIT_YN
                          FROM MU_PAYMENT AS MP
                    INNER JOIN MU_PAYMENT_DETAIL AS MPD
                            ON MP.MU_PAYMENT_ID = MPD.MU_PAYMENT_ID
                           AND MP.USE_YN = 'Y'
                           AND MPD.USE_YN = 'Y'
                           AND MP.STATUS != 'C'
                           AND MPD.MU_RESERVE_ID IS NOT NULL
                           AND MPD.MU_RESERVE_ID IS NOT NULL
                           AND MP.PAYMENT_CATEGORY_ITEM_ID != 'MM'
                           AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                  ) AS MP
            INNER JOIN MU_RESERVE AS MR
                    ON MR.MU_RESERVE_ID = MP.MU_RESERVE_ID
                   AND MR.USE_YN = 'Y'
          ) AS MP
    INNER JOIN MU_USER AS MU
            ON MP.MU_USER_ID = MU.MU_USER_ID
           AND MU.USE_YN = 'Y'
    </select>

    <select id="selectTotalCountAmountPush" parameterType="hashmap" resultType="hashmap">
        SELECT MSKUPH.COUNT   AS "totalCount"
              ,MSKUPH.AMOUNT  AS "totalAmount"
          FROM (
                SELECT COUNT(*)   AS COUNT
                      ,0          AS AMOUNT
                  FROM MU_PUSH AS MP
                 WHERE MP.USE_YN = 'Y'
                   AND MP.STATE = 'Y'
                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
        
                UNION ALL
        
                SELECT COUNT(*)                   AS COUNT
                      ,IFNULL(SUM(MS.AMOUNT), 0)  AS AMOUNT
                  FROM (
                        SELECT MS.TYPE   AS SMS_TYPE
                              ,CASE
                               WHEN MS.TYPE = 'S'
                               THEN 16
                               WHEN MS.TYPE = 'L'
                               THEN 42
                               ELSE 0
                               END                      AS AMOUNT
                          FROM MU_SMS AS MS
                         WHERE MS.USE_YN = 'Y'
                           AND MS.STATE = 'Y'
                           AND MS.POPBILL_STATE = '3'
                           AND MS.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MS.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                  ) AS MS
        
                UNION ALL
        
                SELECT COUNT(*)                   AS COUNT
                      ,IFNULL(SUM(MK.AMOUNT), 0)  AS AMOUNT
                  FROM (
                        SELECT 10                      AS AMOUNT
                          FROM MU_KAKAO AS MK
                         WHERE MK.USE_YN = 'Y'
                           AND MK.STATE = 'Y'
                           AND MK.POPBILL_STATE = '3'
                           AND MK.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MK.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                  ) AS MK
          ) AS MSKUPH
    </select>

    <select id="selectStatisticsThisAppPush" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT #{thisYearMonth} AS RESULT_DATE

            UNION ALL

            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
              FROM T_TEMP_DATES
             WHERE RESULT_DATE &lt; LAST_DAY(#{thisYearMonth})
        )
        SELECT TTD.RESULT_DATE                                            AS "resultDate"
              ,CONCAT(TTD.MONTH, '.', TTD.DAY, '(', TTD.DAY_OF_WEEK, ')') AS "date"
              ,IFNULL(MP.TOTAL_COUNT, 0)                                AS "totalCount"
          FROM (
                SELECT TTD.RESULT_DATE                  AS RESULT_DATE
                      ,MONTH(TTD.RESULT_DATE)           AS MONTH
                      ,DAY(TTD.RESULT_DATE)             AS DAY
                      ,CASE DAYOFWEEK(TTD.RESULT_DATE)
                       WHEN 1 THEN '일'
                       WHEN 2 THEN '월'
                       WHEN 3 THEN '화'
                       WHEN 4 THEN '수'
                       WHEN 5 THEN '목'
                       WHEN 6 THEN '금'
                       WHEN 7 THEN '토'
                       END                           AS DAY_OF_WEEK
                  FROM T_TEMP_DATES AS TTD
              ORDER BY RESULT_DATE ASC
          ) TTD
     LEFT JOIN (
                SELECT MP.WRITE_DATE  AS WRITE_DATE
                      ,COUNT(*)         AS TOTAL_COUNT
                  FROM MU_PUSH AS MP
                 WHERE MP.USE_YN = 'Y'
                   AND MP.STATE = 'Y'
                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
              GROUP BY MP.WRITE_DATE
     ) AS MP
            ON TTD.RESULT_DATE = MP.WRITE_DATE
    </select>

    <select id="selectStatisticsThisSms" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT #{thisYearMonth} AS RESULT_DATE

            UNION ALL

            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
              FROM T_TEMP_DATES
             WHERE RESULT_DATE &lt; LAST_DAY(#{thisYearMonth})
        )
        SELECT TTD.RESULT_DATE                                            AS "resultDate"
              ,CONCAT(TTD.MONTH, '.', TTD.DAY, '(', TTD.DAY_OF_WEEK, ')') AS "date"
              ,IFNULL(MS.TOTAL_COUNT, 0)                                  AS "totalCount"
          FROM (
                SELECT TTD.RESULT_DATE                  AS RESULT_DATE
                      ,MONTH(TTD.RESULT_DATE)           AS MONTH
                      ,DAY(TTD.RESULT_DATE)             AS DAY
                      ,CASE DAYOFWEEK(TTD.RESULT_DATE)
                       WHEN 1 THEN '일'
                       WHEN 2 THEN '월'
                       WHEN 3 THEN '화'
                       WHEN 4 THEN '수'
                       WHEN 5 THEN '목'
                       WHEN 6 THEN '금'
                       WHEN 7 THEN '토'
                       END                           AS DAY_OF_WEEK
                  FROM T_TEMP_DATES AS TTD
              ORDER BY RESULT_DATE ASC
          ) TTD
     LEFT JOIN (
                SELECT MS.WRITE_DATE  AS WRITE_DATE
                      ,COUNT(*)       AS TOTAL_COUNT
                  FROM MU_SMS AS MS
                 WHERE MS.USE_YN = 'Y'
                   AND MS.STATE = 'Y'
                   AND MS.POPBILL_STATE = '3'
                   AND MS.WRITE_DATE &gt;= #{thisYearMonth}
                   AND MS.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
              GROUP BY MS.WRITE_DATE
     ) AS MS
            ON TTD.RESULT_DATE = MS.WRITE_DATE
    </select>

    <select id="selectStatisticsThisKakao" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT #{thisYearMonth} AS RESULT_DATE

            UNION ALL

            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
              FROM T_TEMP_DATES
             WHERE RESULT_DATE &lt; LAST_DAY(#{thisYearMonth})
        )
        SELECT TTD.RESULT_DATE                                            AS "resultDate"
              ,CONCAT(TTD.MONTH, '.', TTD.DAY, '(', TTD.DAY_OF_WEEK, ')') AS "date"
              ,IFNULL(MK.TOTAL_COUNT, 0)                                  AS "totalCount"
          FROM (
                SELECT TTD.RESULT_DATE                  AS RESULT_DATE
                      ,MONTH(TTD.RESULT_DATE)           AS MONTH
                      ,DAY(TTD.RESULT_DATE)             AS DAY
                      ,CASE DAYOFWEEK(TTD.RESULT_DATE)
                       WHEN 1 THEN '일'
                       WHEN 2 THEN '월'
                       WHEN 3 THEN '화'
                       WHEN 4 THEN '수'
                       WHEN 5 THEN '목'
                       WHEN 6 THEN '금'
                       WHEN 7 THEN '토'
                       END                           AS DAY_OF_WEEK
                  FROM T_TEMP_DATES AS TTD
              ORDER BY RESULT_DATE ASC
          ) TTD
     LEFT JOIN (
                SELECT MK.WRITE_DATE  AS WRITE_DATE
                      ,COUNT(*)       AS TOTAL_COUNT
                  FROM MU_KAKAO AS MK
                 WHERE MK.USE_YN = 'Y'
                   AND MK.STATE = 'Y'
                   AND MK.POPBILL_STATE = '3'
                   AND MK.WRITE_DATE &gt;= #{thisYearMonth}
                   AND MK.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
              GROUP BY MK.WRITE_DATE
     ) AS MK
            ON TTD.RESULT_DATE = MK.WRITE_DATE
    </select>

    <select id="selectStatisticsThisTotalAmount" parameterType="hashmap" resultType="hashmap">
        WITH RECURSIVE T_TEMP_DATES AS (
            SELECT #{thisYearMonth} AS RESULT_DATE

            UNION ALL

            SELECT DATE_ADD(RESULT_DATE, INTERVAL 1 DAY)
              FROM T_TEMP_DATES
             WHERE RESULT_DATE &lt; LAST_DAY(#{thisYearMonth})
        )
        SELECT TTD.RESULT_DATE                                            AS "resultDate"
              ,CONCAT(TTD.MONTH, '/', TTD.DAY, '(', TTD.DAY_OF_WEEK, ')') AS "date"
              ,IFNULL(MSK.TOTAL_AMOUNT, 0)                                AS "totalAmount"
          FROM (
                SELECT TTD.RESULT_DATE                  AS RESULT_DATE
                      ,MONTH(TTD.RESULT_DATE)           AS MONTH
                      ,DAY(TTD.RESULT_DATE)             AS DAY
                      ,CASE DAYOFWEEK(TTD.RESULT_DATE)
                       WHEN 1 THEN '일'
                       WHEN 2 THEN '월'
                       WHEN 3 THEN '화'
                       WHEN 4 THEN '수'
                       WHEN 5 THEN '목'
                       WHEN 6 THEN '금'
                       WHEN 7 THEN '토'
                       END                           AS DAY_OF_WEEK
                  FROM T_TEMP_DATES AS TTD
              ORDER BY RESULT_DATE ASC
          ) TTD
     LEFT JOIN (
                SELECT MSK.WRITE_DATE            AS WRITE_DATE
                      ,SUM(MSK.TOTAL_AMOUNT)     AS TOTAL_AMOUNT
                  FROM (
                        SELECT MS.WRITE_DATE     AS WRITE_DATE
                              ,SUM(MS.AMOUNT)    AS TOTAL_AMOUNT
                          FROM (
                                SELECT MS.WRITE_DATE       AS WRITE_DATE
                                      ,CASE
                                       WHEN MS.TYPE = 'S'
                                       THEN 16
                                       WHEN MS.TYPE = 'L'
                                       THEN 42
                                       END                 AS AMOUNT
                                  FROM MU_SMS AS MS
                                 WHERE MS.USE_YN = 'Y'
                                   AND MS.STATE = 'Y'
                          ) AS MS
                      GROUP BY MS.WRITE_DATE

                     UNION ALL

                        SELECT MK.WRITE_DATE     AS WRITE_DATE
                              ,SUM(MK.AMOUNT)    AS TOTAL_AMOUNT
                          FROM (
                                SELECT MK.WRITE_DATE      AS WRITE_DATE
                                      ,10                 AS AMOUNT
                                  FROM MU_KAKAO AS MK
                                 WHERE MK.USE_YN = 'Y'
                                   AND MK.STATE = 'Y'
                          ) AS MK
                      GROUP BY MK.WRITE_DATE
                  ) AS MSK
              GROUP BY MSK.WRITE_DATE
     ) AS MSK
            ON TTD.RESULT_DATE = MSK.WRITE_DATE
    </select>

    <select id="selectStatisticsPushState" parameterType="hashmap" resultType="hashmap">
        SELECT CASE
               WHEN MSKUPH.STATE = 'Y'
               THEN '성공'
               WHEN MSKUPH.STATE = 'N'
               THEN '실패'
               ELSE ''
               END                      AS "state"
              ,COUNT(*)                 AS "totalCount"
          FROM (
                SELECT CASE
                       WHEN MS.POPBILL_STATE = '3'
                       THEN 'Y'
                       ELSE 'N'
                       END                 AS STATE
                  FROM MU_SMS AS MS
                 WHERE MS.USE_YN = 'Y'
                   AND MS.WRITE_DATE &gt;= #{thisYearMonth}
                   AND MS.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})

             UNION ALL

                SELECT CASE
                       WHEN MK.POPBILL_STATE = '3'
                       THEN 'Y'
                       ELSE 'N'
                       END                 AS STATE
                  FROM MU_KAKAO AS MK
                 WHERE MK.USE_YN = 'Y'
                   AND MK.WRITE_DATE &gt;= #{thisYearMonth}
                   AND MK.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})

             UNION ALL

                SELECT CASE
                       WHEN MP.STATE = 'Y'
                       THEN 'Y'
                       ELSE 'N'
                       END            AS STATE
                  FROM MU_PUSH AS MP
                 WHERE MP.USE_YN = 'Y'
                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
          ) AS MSKUPH
      GROUP BY MSKUPH.STATE
    </select>

    <select id="selectStatisticsKakaoTemplateCountYn" parameterType="hashmap" resultType="hashmap">
        SELECT MK.TEMPLATE_CODE                 AS "templateCode"
              ,MK.TEMPLATE_TITLE                AS "templateTitle"
              ,MK.SUCCESS_COUNT                 AS "successCount"
              ,MK.FAIL_COUNT                    AS "failCount"
              ,MK.TOTAL_COUNT                   AS "totalCount"
              ,MK.SUCCESS_COUNT * MK.AMOUNT     AS "totalAmount"
          FROM (
                SELECT MK.TEMPLATE_CODE                                 AS TEMPLATE_CODE
                      ,(SELECT MKT.TITLE
                          FROM MU_KAKAO_TEMPLATE AS MKT
                         WHERE MKT.USE_YN = 'Y'
                           AND MKT.TEMPLATE_CODE = MK.TEMPLATE_CODE
                         LIMIT 0, 1)                                    AS TEMPLATE_TITLE
                      ,MK.SUCCESS_COUNT                                 AS SUCCESS_COUNT
                      ,MK.FAIL_COUNT                                    AS FAIL_COUNT
                      ,MK.SUCCESS_COUNT + MK.FAIL_COUNT                 AS TOTAL_COUNT
                      ,10                                               AS AMOUNT
                  FROM (
                        SELECT MK.TEMPLATE_CODE                                    AS TEMPLATE_CODE
                              ,COUNT(CASE WHEN MK.POPBILL_STATE = '3' THEN 1 END)  AS SUCCESS_COUNT
                              ,COUNT(CASE WHEN MK.POPBILL_STATE != '3' THEN 1 END) AS FAIL_COUNT
                          FROM MU_KAKAO AS MK
                         WHERE MK.USE_YN = 'Y'
                           AND MK.WRITE_DATE &gt;= #{thisYearMonth}
                           AND MK.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                      GROUP BY MK.TEMPLATE_CODE
                  ) AS MK
          ) AS MK
      ORDER BY TOTAL_COUNT DESC
    </select>

    <select id="selectListStatisticsPush" parameterType="hashmap" resultType="hashmap">
        SELECT DATE_FORMAT(MSKUPH.WRITE_DATE, '%Y-%m-%d')    AS "writeDate"
              ,CASE DAYOFWEEK(MSKUPH.WRITE_DATE)
               WHEN 1 THEN '일'
               WHEN 2 THEN '월'
               WHEN 3 THEN '화'
               WHEN 4 THEN '수'
               WHEN 5 THEN '목'
               WHEN 6 THEN '금'
               WHEN 7 THEN '토'
               END                                           AS "dayOfWeek"
              ,MSKUPH.SEND_TYPE                              AS "sendType"
              ,MSKUPH.TYPE                                   AS "type"
              ,MSKUPH.CONTENT                                AS "content"
              ,MSKUPH.SUCCESS_COUNT                          AS "successCount"
              ,MSKUPH.FAIL_COUNT                             AS "failCount"
              ,MSKUPH.TOTAL_COUNT                            AS "totalCount"
              ,MSKUPH.TOTAL_AMOUNT                           AS "totalAmount"
          FROM (
                SELECT MS.WRITE_DATE                 AS WRITE_DATE
                      ,MS.SEND_TYPE                  AS SEND_TYPE
                      ,MS.TYPE                       AS TYPE
                      ,MS.CONTENT                    AS CONTENT
                      ,MS.SUCCESS_COUNT              AS SUCCESS_COUNT
                      ,MS.FAIL_COUNT                 AS FAIL_COUNT
                      ,MS.TOTAL_COUNT                AS TOTAL_COUNT
                      ,SUCCESS_COUNT * MS.AMOUNT     AS TOTAL_AMOUNT
                  FROM (
                        SELECT MS.WRITE_DATE                             AS WRITE_DATE
                              ,MS.SEND_TYPE                              AS SEND_TYPE
                              ,CASE
                               WHEN MS.TYPE = 'L'
                               THEN 'LMS'
                               WHEN MS.TYPE = 'S'
                               THEN 'SMS'
                               ELSE ''
                               END                                       AS TYPE
                              ,CASE
                               WHEN MS.TYPE = 'S'
                               THEN 16
                               WHEN MS.TYPE = 'L'
                               THEN 42
                               ELSE 0
                               END                                       AS AMOUNT
                              ,MS.CONTENT                                AS CONTENT
                              ,MS.SUCCESS_COUNT                          AS SUCCESS_COUNT
                              ,MS.FAIL_COUNT                             AS FAIL_COUNT
                              ,MS.SUCCESS_COUNT + MS.FAIL_COUNT          AS TOTAL_COUNT
                          FROM (
                                SELECT MS.WRITE_DATE                                        AS WRITE_DATE
                                      ,'SMS'                                                AS SEND_TYPE
                                      ,MS.TYPE                                              AS TYPE
                                      ,MS.CONTENT                                           AS CONTENT
                                      ,MS.RECEIPT_NUMBER                                    AS RECEIPT_NUMBER
                                      ,COUNT(CASE WHEN MS.POPBILL_STATE = '3' THEN 1 END)   AS SUCCESS_COUNT
                                      ,COUNT(CASE WHEN MS.POPBILL_STATE != '3' THEN 1 END)  AS FAIL_COUNT
                                  FROM MU_SMS AS MS
                                 WHERE MS.USE_YN = 'Y'
                                   AND MS.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MS.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MS.WRITE_DATE
                                      ,MS.TYPE
                                      ,MS.CONTENT
                                      ,MS.RECEIPT_NUMBER
                          ) AS MS
                  ) AS MS

             UNION ALL

                SELECT MK.WRITE_DATE                 AS WRITE_DATE
                      ,MK.SEND_TYPE                  AS SEND_TYPE
                      ,MK.TYPE                       AS TYPE
                      ,MK.CONTENT                    AS CONTENT
                      ,MK.SUCCESS_COUNT              AS SUCCESS_COUNT
                      ,MK.FAIL_COUNT                 AS FAIL_COUNT
                      ,MK.TOTAL_COUNT                AS TOTAL_COUNT
                      ,MK.SUCCESS_COUNT * MK.AMOUNT  AS TOTAL_AMOUNT
                  FROM (
                        SELECT MK.WRITE_DATE                             AS WRITE_DATE
                              ,MK.SEND_TYPE                              AS SEND_TYPE
                              ,(SELECT MKT.TITLE
                                  FROM MU_KAKAO_TEMPLATE AS MKT
                                 WHERE MKT.USE_YN = 'Y'
                                   AND MKT.TEMPLATE_CODE = MK.TEMPLATE_CODE
                                 LIMIT 0, 1)                             AS TYPE
                              ,10                                        AS AMOUNT
                              ,MK.CONTENT                                AS CONTENT
                              ,MK.SUCCESS_COUNT                          AS SUCCESS_COUNT
                              ,MK.FAIL_COUNT                             AS FAIL_COUNT
                              ,MK.SUCCESS_COUNT + MK.FAIL_COUNT          AS TOTAL_COUNT
                          FROM (
                                SELECT MK.WRITE_DATE                                        AS WRITE_DATE
                                      ,'알림톡'                                              AS SEND_TYPE
                                      ,MK.TEMPLATE_CODE                                     AS TEMPLATE_CODE
                                      ,MK.CONTENT                                           AS CONTENT
                                      ,MK.RECEIPT_NUMBER                                    AS RECEIPT_NUMBER
                                      ,COUNT(CASE WHEN MK.POPBILL_STATE = '3' THEN 1 END)   AS SUCCESS_COUNT
                                      ,COUNT(CASE WHEN MK.POPBILL_STATE != '3' THEN 1 END)  AS FAIL_COUNT
                                  FROM MU_KAKAO AS MK
                                 WHERE MK.USE_YN = 'Y'
                                   AND MK.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MK.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MK.WRITE_DATE
                                      ,MK.TEMPLATE_CODE
                                      ,MK.CONTENT
                                      ,MK.RECEIPT_NUMBER
                          ) AS MK
                  ) AS MK

             UNION ALL

                SELECT MP.WRITE_DATE                   AS WRITE_DATE
                      ,MP.SEND_TYPE                    AS SEND_TYPE
                      ,MP.TYPE                         AS TYPE
                      ,MP.CONTENT                      AS CONTENT
                      ,MP.SUCCESS_COUNT                AS SUCCESS_COUNT
                      ,MP.FAIL_COUNT                   AS FAIL_COUNT
                      ,MP.TOTAL_COUNT                  AS TOTAL_COUNT
                      ,MP.SUCCESS_COUNT * MP.AMOUNT  AS TOTAL_AMOUNT
                  FROM (
                        SELECT MP.WRITE_DATE                             AS WRITE_DATE
                              ,MP.SEND_TYPE                              AS SEND_TYPE
                              ,MP.TYPE                                   AS TYPE
                              ,0                                           AS AMOUNT
                              ,MP.CONTENT                                AS CONTENT
                              ,MP.SUCCESS_COUNT                          AS SUCCESS_COUNT
                              ,MP.FAIL_COUNT                             AS FAIL_COUNT
                              ,MP.SUCCESS_COUNT + MP.FAIL_COUNT        AS TOTAL_COUNT
                          FROM (
                                SELECT MP.WRITE_DATE                                AS WRITE_DATE
                                      ,'앱 푸시'                                     AS SEND_TYPE
                                      ,CASE
                                       WHEN MP.PUSH_TYPE = 'A'
                                       THEN '자동발송'
                                       WHEN MP.PUSH_TYPE = 'MKT'
                                       THEN '마케팅'
                                        END                                           AS TYPE
                                      ,MP.CONTENT                                   AS CONTENT
                                      ,MP.T_ID                                      AS T_ID
                                      ,COUNT(CASE WHEN MP.STATE = 'Y' THEN 1 END)   AS SUCCESS_COUNT
                                      ,COUNT(CASE WHEN MP.STATE != 'Y' THEN 1 END)  AS FAIL_COUNT
                                  FROM MU_PUSH AS MP
                                 WHERE MP.USE_YN = 'Y'
                                   AND MP.WRITE_DATE &gt;= #{thisYearMonth}
                                   AND MP.WRITE_DATE &lt;= LAST_DAY(#{thisYearMonth})
                              GROUP BY MP.WRITE_DATE
                                      ,MP.CONTENT
                                      ,MP.T_ID
                          ) AS MP
                  ) AS MP
          ) AS MSKUPH
      ORDER BY MSKUPH.WRITE_DATE DESC
    </select>

</mapper>