<?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.RptDailyOverspeedCompanyMapper">
    <select id="report" resultType="com.ningdatech.carapi.car.rpt.model.po.RptDailyOverspeedCompanyPO">
        SELECT
            company_id,
            company_name,
            region_id,
            vehicleNums,
            overspeedVehicleNums,
            overspeedRate,
            ( @rank := @rank + 1 ) rank
        FROM(
            SELECT
                company_id,
                company_name,
                region_id,
                vehicleNums,
                overspeedVehicleNums,
                overspeedRate
            FROM(
        SELECT
            o.company_id,
            c.NAME company_name,
            o.region_id,
            c.vehicle_num vehicleNums,
            count( o.car_plate ) overspeedVehicleNums,
            IFNULL( round( count( o.car_plate ) / c.vehicle_num * 100, 2 ), 0 ) overspeedRate
        FROM
            nd_vehicle_overspeed_info o
            LEFT JOIN nd_region r ON o.region_id = r.id
            LEFT JOIN nd_company_info c ON o.company_id = c.id
        where 1 = 1
        <if test="param.date != null and param.date != ''">
            and overspeed_date between concat(#{param.date},' 00:00:00') and
            concat(#{param.date},' 23:59:59')
        </if>
        ${param.dataScopeSql}
        GROUP BY  o.company_id,c.NAME,o.region_id
        ORDER BY  IFNULL( round( count( o.id ) / c.vehicle_num * 100, 2 ), 0 ) DESC,
                overspeedVehicleNums DESC,c.vehicle_num desc
        ) ew
                where vehicleNums is not null
                <if test="param.startRate != null">
                   and overspeedRate &gt;= #{param.startRate}
                </if>
                <if test="param.endRate != null">
                   and overspeedRate &lt;= #{param.endRate}
                </if>

        ) ew,
        ( SELECT @rank := 0 ) t
        where 1 = 1
        <if test="param.rank != null">
            and @rank &lt; #{param.rank }
        </if>
    </select>
</mapper>
