<?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.seer.teach.iot.module.mapper.DevicesMapper">

    <select id="selectCityPercentages" resultType="com.seer.teach.iot.common.dto.CityPercentageDTO">
        SELECT
        city_id,
        COUNT(*) AS device_count,
        ROUND(COUNT(*) * 100.0 / (
        SELECT COUNT(*)
        FROM iot_devices
        WHERE deleted = 0
        ), 2) AS percentage
        FROM iot_devices
        WHERE deleted = 0
        AND city_id IN
        <foreach collection="cityIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY city_id
        ORDER BY city_id;
    </select>
</mapper>
