<?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.mysql.newalgorithm.RStatSubstationDPOMapper">

    <select id="getSumAndCount" resultType="java.util.Map">
        SELECT t.effectiveMeasurementCountAvg,
               t.harmonicCountCount,
               t.freqCountCount,
               t.vDevCountCount,
               t.vCountCount,
               t.iCountCount,
               t.unbalanceCountCount,
               t.iNegCountCount,
               t.flickerCountCount,
               t.inuharmCountCount,
               t.eventCountSum,
               CASE

                   WHEN t.eventMeasurementCountSum = 0 THEN
                       0.00
                   ELSE round(t.eventCountSum / t.eventMeasurementCountSum, 2)
                   END "eventCountFrequency",
               t.sagCountSum,
               CASE

                   WHEN t.sagMeasurementCountSum = 0 THEN
                       0.00
                   ELSE round(t.sagCountSum / t.sagMeasurementCountSum, 2)
                   END "sagCountFrequency",
               t.swellCountSum,
               CASE

                   WHEN t.swellMeasurementCountSum = 0 THEN
                       0.00
                   ELSE round(t.swellCountSum / t.swellMeasurementCountSum, 2)
                   END "swellCountFrequency",
               t.interruptCountSum,
               CASE

                   WHEN t.interruptMeasurementCountSum = 0 THEN
                       0.00
                   ELSE round(t.interruptCountSum / t.interruptMeasurementCountSum, 2)
                   END "interruptCountFrequency"
        FROM (
                 SELECT round(avg(effective_measurement_count), 2)      "effectiveMeasurementCountAvg",
                        count(CASE WHEN harmonic_count > 0 THEN 1 END)  "harmonicCountCount",
                        count(CASE WHEN freq_count > 0 THEN 1 END)      "freqCountCount",
                        count(CASE WHEN v_dev_count > 0 THEN 1 END)     "vDevCountCount",
                        count(CASE WHEN v_count > 0 THEN 1 END)         "vCountCount",
                        count(CASE WHEN i_count > 0 THEN 1 END)         "iCountCount",
                        count(CASE WHEN unbalance_count > 0 THEN 1 END) "unbalanceCountCount",
                        count(CASE WHEN i_neg_count > 0 THEN 1 END)     "iNegCountCount",
                        count(CASE WHEN flicker_count > 0 THEN 1 END)   "flickerCountCount",
                        count(CASE WHEN inuharm_count > 0 THEN 1 END)   "inuharmCountCount",
                        sum(event_count)                                "eventCountSum",
                        sum(event_measurement_count)                    "eventMeasurementCountSum",
                        sum(sag_count)                                  "sagCountSum",
                        sum(sag_measurement_count)                      "sagMeasurementCountSum",
                        sum(swell_count)                                "swellCountSum",
                        sum(swell_measurement_count)                    "swellMeasurementCountSum",
                        sum(interrupt_count)                            "interruptCountSum",
                        sum(interrupt_measurement_count)                "interruptMeasurementCountSum"
                 FROM r_stat_substation_d
                 WHERE substation_id = #{substationId}
                   AND data_date between #{startTime} and #{endTime}
             ) t
    </select>

    <select id="getSumAndCountQ" resultType="java.util.Map">
        SELECT sum(event_measurement_count)     "eventMeasurementCountSum",
               sum(sag_measurement_count)       "sagMeasurementCountSum",
               sum(swell_measurement_count)     "swellMeasurementCountSum",
               sum(interrupt_measurement_count) "interruptMeasurementCountSum"
        FROM r_stat_substation_d
        WHERE data_date between #{startTime} and #{endTime}
    </select>

</mapper>