<?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.performance.mapper.SalePerformanceDetailMapper">

    <select id="querySaleGroupAdByUser" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t1.advertiser_id as ad,
               t2.name,
               sum(t1.total_cost) as totalCost,
               sum(case when brand_new = 0
                            then t1.total_cost else 0 end ) 'oldCost',
               sum(case when brand_new = 1
                            then t1.total_cost else 0 end ) 'newCost',
               sum(case when brand_new = 0
                            then t1.percent else 0 end ) 'oldPercent',
               sum(case when brand_new = 1
                            then t1.percent else 0 end ) 'newPercent'
        FROM
            ctop_sale_performance_detail t1
                LEFT JOIN ctop_advertiser t2 on t1.advertiser_id=t2.id
        WHERE
            t1.sale_id = #{userId}
          AND t1.YEAR = #{year}
          AND t1.QUARTER = #{quarter}
        GROUP BY
            t1.advertiser_id
    </select>

</mapper>
