<?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.njcn.prepare.harmonic.mapper.oracle.DataVOraclePOMapper">

    <select id="getFiveItemsForDataException" resultType="com.njcn.prepare.harmonic.pojo.vo.DataVOracleVO">
        SELECT
        a.*,
        b.*
        FROM
        (
        SELECT
        LINEID,
        AVG( RMS_MAX ) AS rmsMax,
        AVG( RMS_MIN ) AS rmsMin,
        AVG( V_THD_MAX ) AS vThdMax,
        AVG( V_THD_MIN ) AS vThdMin
        FROM
        DATA_V
        WHERE
        <![CDATA[TIMEID >= #{startTime}]]>
        AND <![CDATA[TIMEID <= #{endTime}]]>
        AND LINEID = #{lineId}
        AND PHASIC_TYPE IN ( 'A', 'B', 'C' )
        GROUP BY
        LINEID
        ) a
        LEFT JOIN (
        SELECT
        LINEID,
        AVG( FREQ_MAX ) AS freqMax,
        AVG( FREQ_MIN ) AS freqMin,
        AVG( RMSAB_MAX ) + AVG( RMSBC_MAX ) + AVG( RMSCA_MAX ) AS rmsLvrMax,
        AVG( RMSAB_MIN ) + AVG( RMSBC_MIN ) + AVG( RMSCA_MIN ) AS rmsLvrMin,
        AVG( V_UNBALANCE_MAX ) AS vUnbalanceMax,
        AVG( V_UNBALANCE_MIN ) AS vUnbalanceMin
        FROM
        DATA_V
        WHERE
        <![CDATA[TIMEID >= #{startTime}]]>
        AND <![CDATA[TIMEID <= #{endTime}]]>
        AND LINEID = #{lineId}
        AND PHASIC_TYPE = 'T'
        GROUP BY
        LINEID
        ) b ON a.LINEID = b.LINEID
    </select>

    <select id="getFiveCountForDataIntegrityRate" resultType="com.njcn.prepare.harmonic.pojo.vo.DataVOracleVO">
        SELECT
        LINEID,
        COUNT( FREQ ) AS freqCount,
        COUNT( RMS ) AS rmsCount,
        COUNT( RMSAB ) + COUNT( RMSBC ) + COUNT( RMSCA ) AS rmsLvrCount,
        COUNT( V_THD ) AS vThdCount,
        COUNT( V_UNBALANCE ) AS vUnbalanceCount
        FROM
        DATA_V
        WHERE
        <![CDATA[TIMEID >= #{startTime}]]>
        AND <![CDATA[TIMEID <= #{endTime}]]>
        AND LINEID = #{lineId}
        AND PHASIC_TYPE = 'A'
        GROUP BY
        LINEID
    </select>
    <select id="getFiveItems" resultType="com.njcn.prepare.harmonic.pojo.dto.DataVFiveItemDTO">
select a.lineid,a.timeid,a.rmsMax,a.rmsMin,a.vThdMax,a.vThdMin,
 b.freqMax,b.freqMin,b.rmsLvrMax,b.rmsLvrMin,b.vUnbalanceMax,b.vUnbalanceMin from
(SELECT lineid,
       timeid,
       Max(RMS_MAX) AS rmsMax,
       Min(RMS_MIN) AS rmsMin,
       Max(V_THD_MAX) AS vThdMax,
       Min(V_THD_MIN) AS vThdMin
  FROM DATA_V
 WHERE lineid = #{lineId}
   and TO_CHAR(TIMEID, 'yyyy-mm-dd') = #{date}
   AND PHASIC_TYPE IN ('A', 'B', 'C')
 group by lineid, TIMEID) a left join
(SELECT lineid,
       timeid,
       Max(FREQ_MAX) AS freqMax,
       Min(FREQ_MIN) AS freqMin,
       GREATEST(MaX(RMSAB_MAX), MaX(RMSBC_MAX), MAX(RMSCA_MAX)) AS rmsLvrMax,
       least(MIN(RMSAB_MIN), MIN(RMSBC_MIN), MIN(RMSCA_MIN)) AS rmsLvrMin,
       Max(V_UNBALANCE_MAX) AS vUnbalanceMax,
       Min(V_UNBALANCE_MIN) AS vUnbalanceMin
  FROM DATA_V
 WHERE lineid = #{lineId}
   and TO_CHAR(TIMEID, 'yyyy-mm-dd') =  #{date}
   AND PHASIC_TYPE = 'T'
 group by lineid, TIMEID) b
 on a.LINEID = b.LINEID and a.timeid =b.timeid
    </select>
</mapper>