<?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.RStatSubstationMPOMapper">

    <select id="getSumAndCount" resultType="java.util.Map">
        SELECT t.effectiveMeasurementAverageAvg,
               CASE

                   WHEN t.harmonicCountSum = 0 THEN
                       0.00
                   ELSE round(t.harmonicOverDaySum / t.harmonicCountSum, 2)
                   END "harmonicAverageOverDay",
               CASE

                   WHEN t.freqCountSum = 0 THEN
                       0.00
                   ELSE round(t.freqOverDaySum / t.freqCountSum, 2)
                   END "freqAverageOverDay",
               CASE

                   WHEN t.vDevCountSum = 0 THEN
                       0.00
                   ELSE round(t.vDevOverDaySum / t.vDevCountSum, 2)
                   END "vDevAverageOverDay",
               CASE

                   WHEN t.vCountSum = 0 THEN
                       0.00
                   ELSE round(t.vOverDaySum / t.vCountSum, 2)
                   END "vAverageOverDay",
               CASE

                   WHEN t.iCountSum = 0 THEN
                       0.00
                   ELSE round(t.iOverDaySum / t.iCountSum, 2)
                   END "iAverageOverDay",
               CASE

                   WHEN t.unbalanceCountSum = 0 THEN
                       0.00
                   ELSE round(t.unbalanceOverDaySum / t.unbalanceCountSum, 2)
                   END "unbalanceAverageOverDay",
               CASE

                   WHEN t.iNegCountSum = 0 THEN
                       0.00
                   ELSE round(t.iNegOverDaySum / t.iNegCountSum, 2)
                   END "iNegAverageOverDay",
               CASE

                   WHEN t.flickerCountSum = 0 THEN
                       0.00
                   ELSE round(t.flickerOverDaySum / t.flickerCountSum, 2)
                   END "flickerAverageOverDay",
               CASE

                   WHEN t.inuharmCountSum = 0 THEN
                       0.00
                   ELSE round(t.inuharmOverDaySum / t.inuharmCountSum, 2)
                   END "inuharmAverageOverDay",
               t.eventCountSum,
               t.sagCountSum,
               t.swellCountSum,
               t.interruptCountSum
        FROM (
                 SELECT round(avg(effective_measurement_average), 2) "effectiveMeasurementAverageAvg",
                        sum(harmonic_over_day)                       "harmonicOverDaySum",
                        sum(harmonic_count)                          "harmonicCountSum",
                        sum(freq_over_day)                           "freqOverDaySum",
                        sum(freq_count)                              "freqCountSum",
                        sum(v_dev_over_day)                          "vDevOverDaySum",
                        sum(v_dev_count)                             "vDevCountSum",
                        sum(v_over_day)                              "vOverDaySum",
                        sum(v_count)                                 "vCountSum",
                        sum(i_over_day)                              "iOverDaySum",
                        sum(i_count)                                 "iCountSum",
                        sum(unbalance_over_day)                      "unbalanceOverDaySum",
                        sum(unbalance_count)                         "unbalanceCountSum",
                        sum(i_neg_over_day)                          "iNegOverDaySum",
                        sum(i_neg_count)                             "iNegCountSum",
                        sum(flicker_over_day)                        "flickerOverDaySum",
                        sum(flicker_count)                           "flickerCountSum",
                        sum(inuharm_over_day)                        "inuharmOverDaySum",
                        sum(inuharm_count)                           "inuharmCountSum",

                        sum(event_count)                             "eventCountSum",
                        sum(sag_count)                               "sagCountSum",
                        sum(swell_count)                             "swellCountSum",
                        sum(interrupt_count)                         "interruptCountSum"
                 FROM r_stat_substation_m
                 WHERE substation_id = #{substationId}
                   AND data_date between #{startTime} and #{endTime}
             ) t
    </select>

</mapper>