<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.its.web.mapper.its.statistics.StatisticsMapper">
    <select id="findStatisticsCommSpeedMonth" parameterType="java.util.HashMap" resultType="com.its.web.dto.statistics.TrafficStatisticsDto">
        SELECT
            IFSC_NM AS road_name,
            STNODENAME AS strt_name,
            EDNODENAME AS end_name,
            t01,
            t02,
            t03,
            t04,
            t05,
            t06,
            t07,
            t08,
            t09,
            t10,
            t11,
            t12,
            t13,
            t15,
            t14,
            t16,
            t17,
            t18,
            t19,
            t20,
            t21,
            t22,
            t23,
            t24,
            t25,
            t26,
            t27,
            t28,
            t29,
            t30,
            t31,
            DIRECTION,
            ORD1,
            ORD2
        FROM (
                 SELECT
                     F.DRCT_CD AS DIRECTION,
                     B.IFSC_ID AS LINKID,
                     C.IFSC_NM,
                     C.STRT_NM AS STNODENAME,
                     C.END_NM AS EDNODENAME,
                     A.ORD AS ORD1,
                     B.ORD AS ORD2
                 FROM TB_ATRD_ROAD_RLTN A,
                      TB_ROAD_IFSC_RLTN B,
                      TB_IFSC C,
                      TB_NODE D,
                      TB_NODE E,
                      TB_ATRD F,
                      TB_IFSC_TRAF G
                 WHERE F.ATRD_NM = #{ATRD_NM}
                   AND A.ROAD_ID = B.ROAD_ID
                   AND B.IFSC_ID = C.IFSC_ID
                   AND C.F_NODE_ID = D.NODE_ID
                   AND C.T_NODE_ID = E.NODE_ID
                   AND A.ATRD_ID = F.ATRD_ID
                   AND B.IFSC_ID = G.IFSC_ID
                 ORDER BY A.ATRD_ID,A.ORD, B.ORD,F.DRCT_CD
             ) A,
             (
                 SELECT
                     IFSC_ID,
                     t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13, t14,t15,t16,t17,t18,t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30,t31
                 FROM
                     (
                         SELECT
                             B.IFSC_ID,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'01', TO_NUMBER(NVL(SPED, 0)))), 0) AS t01,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'02', TO_NUMBER(NVL(SPED, 0)))), 0) AS t02,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'03', TO_NUMBER(NVL(SPED, 0)))), 0) AS t03,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'04', TO_NUMBER(NVL(SPED, 0)))), 0) AS t04,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'05', TO_NUMBER(NVL(SPED, 0)))), 0) AS t05,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'06', TO_NUMBER(NVL(SPED, 0)))), 0) AS t06,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'07', TO_NUMBER(NVL(SPED, 0)))), 0) AS t07,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'08', TO_NUMBER(NVL(SPED, 0)))), 0) AS t08,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'09', TO_NUMBER(NVL(SPED, 0)))), 0) AS t09,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'10', TO_NUMBER(NVL(SPED, 0)))), 0) AS t10,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'11', TO_NUMBER(NVL(SPED, 0)))), 0) AS t11,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'12', TO_NUMBER(NVL(SPED, 0)))), 0) AS t12,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'13', TO_NUMBER(NVL(SPED, 0)))), 0) AS t13,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'14', TO_NUMBER(NVL(SPED, 0)))), 0) AS t14,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'15', TO_NUMBER(NVL(SPED, 0)))), 0) AS t15,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'16', TO_NUMBER(NVL(SPED, 0)))), 0) AS t16,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'17', TO_NUMBER(NVL(SPED, 0)))), 0) AS t17,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'18', TO_NUMBER(NVL(SPED, 0)))), 0) AS t18,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'19', TO_NUMBER(NVL(SPED, 0)))), 0) AS t19,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'20', TO_NUMBER(NVL(SPED, 0)))), 0) AS t20,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'21', TO_NUMBER(NVL(SPED, 0)))), 0) AS t21,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'22', TO_NUMBER(NVL(SPED, 0)))), 0) AS t22,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'23', TO_NUMBER(NVL(SPED, 0)))), 0) AS t23,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'24', TO_NUMBER(NVL(SPED, 0)))), 0) AS t24,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'25', TO_NUMBER(NVL(SPED, 0)))), 0) AS t25,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'26', TO_NUMBER(NVL(SPED, 0)))), 0) AS t26,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'27', TO_NUMBER(NVL(SPED, 0)))), 0) AS t27,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'28', TO_NUMBER(NVL(SPED, 0)))), 0) AS t28,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'29', TO_NUMBER(NVL(SPED, 0)))), 0) AS t29,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'30', TO_NUMBER(NVL(SPED, 0)))), 0) AS t30,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2),'31', TO_NUMBER(NVL(SPED, 0)))), 0) AS t31
                         FROM (
                                  SELECT * FROM TB_IFSC_HH_STAT
                              ) A,
                              (
                                  SELECT
                                      B.IFSC_ID
                                      B.IFSC_ID,
                                      G.CMTR_GRAD_CD AS TRFGRADE
                                  FROM TB_ATRD_ROAD_RLTN A,
                                       TB_ROAD_IFSC_RLTN B,
                                       TB_IFSC C,
                                       TB_NODE D,
                                       TB_NODE E,
                                       TB_ATRD F,
                                       TB_IFSC_TRAF G
                                  WHERE F.ATRD_NM = #{ATRD_NM}
                                    AND A.ROAD_ID = B.ROAD_ID
                                    AND B.IFSC_ID = C.IFSC_ID
                                    AND C.F_NODE_ID = D.NODE_ID
                                    AND C.T_NODE_ID = E.NODE_ID
                                    AND A.ATRD_ID = F.ATRD_ID
                                    AND B.IFSC_ID = G.IFSC_ID
                              ) B
                         WHERE A.IFSC_ID = B.IFSC_ID
                           AND TO_DATE(A.STAT_DT, 'YYYY-MM-DD HH24:MI:SS')
                       BETWEEN TO_DATE(#{FROM_DT}, 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE(#{TO_DT}, 'YYYY-MM-DD HH24:MI:SS')
                         GROUP BY B.IFSC_ID
                         ORDER BY B.IFSC_ID
                     ) Z
                 GROUP BY
                     IFSC_ID,
                     t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30,t31
             ) B
        WHERE A.LINKID = B.IFSC_ID
        ORDER BY DIRECTION,ORD1,ORD2
    </select>

    <select id="findStatisticsCommSpeedDay" parameterType="java.util.HashMap" resultType="com.its.web.dto.statistics.TrafficStatisticsDto">
        SELECT
            IFSC_NM AS road_name,
            STNODENAME AS strt_name,
            EDNODENAME AS end_name,
            t00,
            t01,
            t02,
            t03,
            t04,
            t05,
            t06,
            t07,
            t08,
            t09,
            t10,
            t11,
            t12,
            t13,
            t14,
            t15,
            t16,
            t17,
            t18,
            t19,
            t20,
            t21,
            t22,
            t23,
            ORD1,
            ORD2,
            DIRECTION
        FROM (
            SELECT
                F.DRCT_CD AS DIRECTION,
                B.IFSC_ID AS LINKID,
                C.IFSC_NM,
                C.STRT_NM AS STNODENAME,
                C.END_NM AS EDNODENAME,
                A.ORD AS ORD1,
                B.ORD AS ORD2
            FROM TB_ATRD_ROAD_RLTN A,
                   TB_ROAD_IFSC_RLTN B,
                   TB_IFSC C,
                   TB_NODE D,
                   TB_NODE E,
                   TB_ATRD F,
                   TB_IFSC_TRAF G
            WHERE F.ATRD_NM = #{ATRD_NM}
                AND A.ROAD_ID = B.ROAD_ID
                AND B.IFSC_ID = C.IFSC_ID
                AND C.F_NODE_ID = D.NODE_ID
                AND C.T_NODE_ID = E.NODE_ID
                AND A.ATRD_ID = F.ATRD_ID
                AND B.IFSC_ID = G.IFSC_ID
            ORDER BY A.ATRD_ID,A.ORD, B.ORD,F.DRCT_CD
        ) A,
        (
            SELECT
                IFSC_ID,
                t00,t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23
            FROM
            (
                SELECT
                    B.IFSC_ID,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'00', TO_NUMBER(NVL(SPED, 0)))), 0) AS t00,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'01', TO_NUMBER(NVL(SPED, 0)))), 0) AS t01,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'02', TO_NUMBER(NVL(SPED, 0)))), 0) AS t02,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'03', TO_NUMBER(NVL(SPED, 0)))), 0) AS t03,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'04', TO_NUMBER(NVL(SPED, 0)))), 0) AS t04,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'05', TO_NUMBER(NVL(SPED, 0)))), 0) AS t05,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'06', TO_NUMBER(NVL(SPED, 0)))), 0) AS t06,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'07', TO_NUMBER(NVL(SPED, 0)))), 0) AS t07,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'08', TO_NUMBER(NVL(SPED, 0)))), 0) AS t08,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'09', TO_NUMBER(NVL(SPED, 0)))), 0) AS t09,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'10', TO_NUMBER(NVL(SPED, 0)))), 0) AS t10,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'11', TO_NUMBER(NVL(SPED, 0)))), 0) AS t11,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'12', TO_NUMBER(NVL(SPED, 0)))), 0) AS t12,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'13', TO_NUMBER(NVL(SPED, 0)))), 0) AS t13,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'14', TO_NUMBER(NVL(SPED, 0)))), 0) AS t14,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'15', TO_NUMBER(NVL(SPED, 0)))), 0) AS t15,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'16', TO_NUMBER(NVL(SPED, 0)))), 0) AS t16,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'17', TO_NUMBER(NVL(SPED, 0)))), 0) AS t17,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'18', TO_NUMBER(NVL(SPED, 0)))), 0) AS t18,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'19', TO_NUMBER(NVL(SPED, 0)))), 0) AS t19,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'20', TO_NUMBER(NVL(SPED, 0)))), 0) AS t20,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'21', TO_NUMBER(NVL(SPED, 0)))), 0) AS t21,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'22', TO_NUMBER(NVL(SPED, 0)))), 0) AS t22,
                    ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2),'23', TO_NUMBER(NVL(SPED, 0)))), 0) AS t23
                FROM (
                    SELECT * FROM TB_IFSC_HH_STAT
                ) A,
                (
                    SELECT
                        B.IFSC_ID,
                        G.CMTR_GRAD_CD AS TRFGRADE
                    FROM TB_ATRD_ROAD_RLTN A,
                       TB_ROAD_IFSC_RLTN B,
                       TB_IFSC C,
                       TB_NODE D,
                       TB_NODE E,
                       TB_ATRD F,
                       TB_IFSC_TRAF G
                    WHERE F.ATRD_NM = #{ATRD_NM}
                    AND A.ROAD_ID = B.ROAD_ID
                    AND B.IFSC_ID = C.IFSC_ID
                    AND C.F_NODE_ID = D.NODE_ID
                    AND C.T_NODE_ID = E.NODE_ID
                    AND A.ATRD_ID = F.ATRD_ID
                    AND B.IFSC_ID = G.IFSC_ID
                ) B
                WHERE A.IFSC_ID = B.IFSC_ID
                  AND TO_DATE(A.STAT_DT, 'YYYY-MM-DD HH24:MI:SS')
              BETWEEN TO_DATE(#{FROM_DT}, 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE(#{TO_DT}, 'YYYY-MM-DD HH24:MI:SS')
                GROUP BY B.IFSC_ID
                ORDER BY B.IFSC_ID
            ) Z
            GROUP BY
            Z.IFSC_ID,
            t00,t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23
        ) B
        WHERE A.LINKID = B.IFSC_ID
        ORDER BY DIRECTION,ORD1,ORD2
    </select>
    <select id="findStatisticsCommGradeMonth" parameterType="java.util.HashMap" resultType="com.its.web.dto.statistics.TrafficStatisticsDto">
        SELECT
            IFSC_NM AS road_name,
            STNODENAME AS strt_name,
            EDNODENAME AS end_name,
            t01,
            t02,
            t03,
            t04,
            t05,
            t06,
            t07,
            t08,
            t09,
            t10,
            t11,
            t12,
            t13,
            t15,
            t16,
            t17,
            t18,
            t19,
            t20,
            t21,
            t22,
            t23,
            t24,
            t25,
            t26,
            t27,
            t28,
            t29,
            t30,
            t31,
            ORD1,
            ORD2,
            DIRECTION
       FROM (
           SELECT
               F.DRCT_CD AS DIRECTION,
               B.IFSC_ID AS LINKID,
               C.IFSC_NM,
               C.STRT_NM AS STNODENAME,
               C.END_NM AS EDNODENAME,
               A.ORD AS ORD1,
               B.ORD AS ORD2
           FROM TB_ATRD_ROAD_RLTN A,
             TB_ROAD_IFSC_RLTN B,
             TB_IFSC C,
             TB_NODE D,
             TB_NODE E,
             TB_ATRD F,
             TB_IFSC_TRAF G
           WHERE F.ATRD_NM = #{ATRD_NM}
               AND A.ROAD_ID = B.ROAD_ID
               AND B.IFSC_ID = C.IFSC_ID
               AND C.F_NODE_ID = D.NODE_ID
               AND C.T_NODE_ID = E.NODE_ID
               AND A.ATRD_ID = F.ATRD_ID
               AND B.IFSC_ID = G.IFSC_ID
           ORDER BY A.ATRD_ID,A.ORD, B.ORD,F.DRCT_CD
       ) A,
       (
           SELECT
               IFSC_ID,
               t01, t02, t03, t04, t05, t06, t07, t08, t09, t10, t11, t12, t13, t14, t15,
               t16, t17, t18, t19, t20, t21, t22, t23, t24, t25, t26, t27, t28, t29, t30, t31
           FROM
           (
               SELECT
                   B.IFSC_ID,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 01, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t01,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 02, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t02,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 03, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t03,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 04, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t04,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 05, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t05,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 06, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t06,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 07, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t07,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 08, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t08,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 09, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t09,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 10, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t10,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 11, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t11,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 12, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t12,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 13, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t13,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 14, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t14,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 15, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t15,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 16, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t16,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 17, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t17,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 18, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t18,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 19, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t19,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 20, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t20,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 21, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t21,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 22, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t22,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 23, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t23,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 24, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t24,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 25, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t25,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 26, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t26,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 27, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t27,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 28, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t28,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 29, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t29,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 30, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t30,
                   ROUND(AVG(DECODE(SUBSTR(STAT_DT,7,2), 31, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t31
               FROM (
                   SELECT * FROM TB_IFSC_HH_STAT
               ) A,
               (
                   SELECT
                       B.IFSC_ID,
                       G.CMTR_GRAD_CD AS TRFGRADE
                   FROM TB_ATRD_ROAD_RLTN A,
                     TB_ROAD_IFSC_RLTN B,
                     TB_IFSC C,
                     TB_NODE D,
                     TB_NODE E,
                     TB_ATRD F,
                     TB_IFSC_TRAF G
                   WHERE F.ATRD_NM = #{ATRD_NM}
                   AND A.ROAD_ID = B.ROAD_ID
                   AND B.IFSC_ID = C.IFSC_ID
                   AND C.F_NODE_ID = D.NODE_ID
                   AND C.T_NODE_ID = E.NODE_ID
                   AND A.ATRD_ID = F.ATRD_ID
                   AND B.IFSC_ID = G.IFSC_ID
               ) B
               WHERE A.IFSC_ID = B.IFSC_ID
                 AND TO_DATE(A.STAT_DT, 'YYYY-MM-DD HH24:MI:SS')
             BETWEEN TO_DATE(#{FROM_DT}, 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE(#{TO_DT}, 'YYYY-MM-DD HH24:MI:SS')
               GROUP BY B.IFSC_ID
               ORDER BY B.IFSC_ID
           ) Z
           GROUP BY
           IFSC_ID,
           t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23,t24,t25,t26,t27,t28,t29,t30,t31
       ) B
       WHERE A.LINKID = B.IFSC_ID
       ORDER BY DIRECTION,ORD1,ORD2
    </select>

    <select id="findStatisticsCommGradeDay" parameterType="java.util.HashMap" resultType="com.its.web.dto.statistics.TrafficStatisticsDto">
        SELECT
            IFSC_NM AS road_name,
            STNODENAME AS strt_name,
            EDNODENAME AS end_name,
            t00,
            t01,
            t02,
            t03,
            t04,
            t05,
            t06,
            t07,
            t08,
            t09,
            t10,
            t11,
            t12,
            t13,
            t15,
            t16,
            t17,
            t18,
            t19,
            t20,
            t21,
            t22,
            t23,
            ORD1,
            ORD2,
            DIRECTION
        FROM (
                 SELECT
                     F.DRCT_CD AS DIRECTION,
                     B.IFSC_ID AS LINKID,
                     C.IFSC_NM,
                     C.STRT_NM AS STNODENAME,
                     C.END_NM AS EDNODENAME,
                     A.ORD AS ORD1,
                     B.ORD AS ORD2
                 FROM TB_ATRD_ROAD_RLTN A,
                      TB_ROAD_IFSC_RLTN B,
                      TB_IFSC C,
                      TB_NODE D,
                      TB_NODE E,
                      TB_ATRD F,
                      TB_IFSC_TRAF G
                 WHERE F.ATRD_NM = #{ATRD_NM}
                   AND A.ROAD_ID = B.ROAD_ID
                   AND B.IFSC_ID = C.IFSC_ID
                   AND C.F_NODE_ID = D.NODE_ID
                   AND C.T_NODE_ID = E.NODE_ID
                   AND A.ATRD_ID = F.ATRD_ID
                   AND B.IFSC_ID = G.IFSC_ID
                 ORDER BY A.ATRD_ID,A.ORD, B.ORD,F.DRCT_CD
             ) A,
             (
                 SELECT
                     IFSC_ID,
                     t00, t01, t02, t03, t04, t05, t06, t07,
                     t08, t09, t10, t11, t12, t13, t14, t15,
                     t16, t17, t18, t19, t20, t21, t22, t23
                 FROM
                     (
                         SELECT
                             B.IFSC_ID,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 01, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t00,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 01, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t01,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 02, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t02,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 03, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t03,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 04, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t04,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 05, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t05,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 06, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t06,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 07, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t07,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 08, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t08,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 09, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t09,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 10, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t10,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 11, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t11,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 12, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t12,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 13, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t13,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 14, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t14,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 15, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t15,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 16, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t16,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 17, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t17,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 18, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t18,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 19, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t19,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 20, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t20,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 21, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t21,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 22, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t22,
                             ROUND(AVG(DECODE(SUBSTR(STAT_DT,9,2), 23, TO_NUMBER(NVL(SUBSTR(TRFGRADE, 4,1), 0)))), 0) AS t23
                         FROM (
                                  SELECT * FROM TB_IFSC_HH_STAT
                              ) A,
                              (
                                  SELECT
                                      B.IFSC_ID,
                                      G.CMTR_GRAD_CD AS TRFGRADE
                                  FROM TB_ATRD_ROAD_RLTN A,
                                       TB_ROAD_IFSC_RLTN B,
                                       TB_IFSC C,
                                       TB_NODE D,
                                       TB_NODE E,
                                       TB_ATRD F,
                                       TB_IFSC_TRAF G
                                  WHERE F.ATRD_NM = #{ATRD_NM}
                                    AND A.ROAD_ID = B.ROAD_ID
                                    AND B.IFSC_ID = C.IFSC_ID
                                    AND C.F_NODE_ID = D.NODE_ID
                                    AND C.T_NODE_ID = E.NODE_ID
                                    AND A.ATRD_ID = F.ATRD_ID
                                    AND B.IFSC_ID = G.IFSC_ID
                              ) B
                         WHERE A.IFSC_ID = B.IFSC_ID
                           AND TO_DATE(A.STAT_DT, 'YYYY-MM-DD HH24:MI:SS')
                       BETWEEN TO_DATE(#{FROM_DT}, 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE(#{TO_DT}, 'YYYY-MM-DD HH24:MI:SS')
                         GROUP BY B.IFSC_ID
                         ORDER BY B.IFSC_ID
                     ) Z
                 GROUP BY
                     IFSC_ID,
                     t00,t01,t02,t03,t04,t05,t06,t07,t08,t09,t10,t11,t12,t13,t14,t15,t16,t17,t18,t19,t20,t21,t22,t23
             ) B
        WHERE A.LINKID = B.IFSC_ID
        ORDER BY DIRECTION,ORD1,ORD2
    </select>

    <select id="findStatisticsCongest" resultType="com.its.web.dto.statistics.TrafficStatisticsCongestDto" parameterType="java.util.HashMap">
        SELECT
            A.IFSC_ID AS LINKID,
            B.IFSC_NM AS road_nm,
            B.STRT_NM AS strt_nm,
            B.END_NM AS end_nm,
            A.CNFS_STRT_HM AS strt_tm,
            A.CNFS_END_HM AS end_tm,
            A.AVRG_SPED AS sped
        FROM TB_REPT_CNGS_SECT A, TB_IFSC B
        WHERE A.IFSC_ID = B.IFSC_ID
        AND DAY_TYPE_CD = #{DAY}
        AND A.STAT_YM > #{STAT_YM}
        ORDER BY A.IFSC_ID DESC
    </select>
</mapper>