<?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.RptTendaysSpecialOverspeedVehiclesMapper">
    <select id="report" resultType="com.ningdatech.carapi.car.rpt.model.po.RptTendaysSpecialOverspeedVehiclesPO">
        select
            car_plate,
            region_id,
            company_id,
            company_name,
            overspeedCurrentTimes,
            overspeedLastTimes,
            overspeedRate,
            (@rank := @rank + 1) rank
        from(select
        car_plate,
        region_id,
        company_id,
        company_name,
        overspeedCurrentTimes,
        overspeedLastTimes,
        (overspeedCurrentTimes - overspeedLastTimes) overspeedRate
        from(
        SELECT
        o.car_plate,
        r.id region_id,
        c.id company_id,
        c.name company_name,
        count(case when o.overspeed_date between #{param.startTime} and #{param.endTime}  then 1 end) overspeedCurrentTimes,
        count(case when o.overspeed_date between #{param.lastStartTime} and #{param.lastEndTime}  then 1 end) overspeedLastTimes
        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,(SELECT @rank:=0) t
        where b.deleted = 0 and b.audit_state = 2 and b.vehicle_type in (1,2,3,4)
        <if test="param.startRate != null">
            and IFNULL((o.max_speed - o.limit_speed)/o.limit_speed*100,0) &gt;= #{param.startRate}
        </if>
        <if test="param.endRate != null">
            and IFNULL((o.max_speed - o.limit_speed)/o.limit_speed*100,0) &lt;= #{param.endRate}
        </if>
        ${param.dataScopeSql}
        group by o.car_plate,r.id,c.id,c.name
        ) ew
        where overspeedCurrentTimes > 0 or overspeedLastTimes > 0
        order by overspeedCurrentTimes desc,
        (overspeedCurrentTimes - overspeedLastTimes) desc) ew
        where 1 = 1
        <if test="param.rank != null">
            and @rank &lt; #{param.rank}
        </if>
    </select>
</mapper>
