<?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.ningdatech.carapi.car.rpt.mapper.RptDailyOverspeedSpecialVehiclesMapper">
    <select id="report" resultType="com.ningdatech.carapi.car.rpt.model.po.RptDailyOverspeedSpecialVehiclesPO">
        SELECT
            car_plate,
            company_id,
            company_name,
            region_id,
            overspeedTotalTimes,
            overspeed2050Times,
            overspeed50Times,
            rate,
            ( @rank := @rank + 1 ) rank
        FROM(
            SELECT
                car_plate,
                company_id,
                company_name,
                region_id,
                overspeedTotalTimes,
                overspeed2050Times,
                overspeed50Times,
                rate
        FROM(
            SELECT
                o.car_plate,
                o.company_id,
                c.NAME company_name,
                o.region_id,
                count( o.id ) overspeedTotalTimes,
                count(case when ((o.max_speed - o.limit_speed)/o.limit_speed) &gt;= 0.2
                                    and (o.max_speed - o.limit_speed)/o.limit_speed &lt;= 0.5 then 1 end) overspeed2050Times,
                count(case when (o.max_speed - o.limit_speed)/o.limit_speed &gt; 0.5 then 1 end) overspeed50Times,
                max(IFNULL(ROUND((o.max_speed - o.limit_speed)/o.limit_speed * 100,2),0)) rate
            FROM
            nd_vehicle_overspeed_info o
            LEFT JOIN nd_vehicle_base_info b ON o.car_plate = b.car_plate
            LEFT JOIN nd_region r ON o.region_id = r.id
            LEFT JOIN nd_company_info c ON o.company_id = c.id
            where b.deleted = 0 and b.audit_state = 2 and b.vehicle_type in (1,2,3,4)
            <if test="param.date != null and param.date != ''">
                and o.overspeed_date between concat(#{param.date},' 00:00:00') and
                concat(#{param.date},' 23:59:59')
            </if>
            ${param.dataScopeSql}
            GROUP BY  o.car_plate,o.company_id,c.NAME,o.region_id
            ORDER BY
                count( o.id ) DESC,
                max(IFNULL(ROUND((o.max_speed - o.limit_speed)/o.limit_speed * 100,2),0)) desc,
                count(case when ((o.max_speed - o.limit_speed)/o.limit_speed) &gt;= 0.2
                    and (o.max_speed - o.limit_speed)/o.limit_speed &lt;= 0.5 then 1 end)  DESC,
                count(case when (o.max_speed - o.limit_speed)/o.limit_speed &gt; 0.5 then 1 end) desc
            ) ew
        ) ew,
        ( SELECT @rank := 0 ) t
        where 1 = 1
        <if test="param.rank != null">
            and @rank &lt; #{param.rank}
        </if>
    </select>
</mapper>
