<?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="org.jeecg.modules.adweb.dmp.mapper.GACountryReportMapper">

    <select id="getCountryStats" resultType="org.jeecg.modules.adweb.dmp.vo.report.CountryStatsVO">
        SELECT
        t1.*,
        LOWER(t2.country_iso_code) AS country_code,
        t2.country_name
        FROM
        (
        SELECT
        country,
        SUM(total_users) AS total_users
        FROM
        adweb_dmp_ga_country_report
        WHERE
        site_code = #{siteCode}
        <if test="start != null">
            AND date >= #{start}
        </if>
        <if test="end != null">
            AND date &lt; DATE_ADD(#{end}, INTERVAL 1 DAY)
        </if>
        GROUP BY
        country
        ) t1
        LEFT JOIN adweb_country t2 ON t1.country = t2.country_name_en
        ORDER BY
        t1.total_users DESC
    </select>

</mapper>