<?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="typereportSqlMapper">
	
	<sql id="base_column">
	   id,
	   operator_id as operatatorId,
	   date,
	   device,
	   pv,
	   uv,
	   impression,
	   click,
	   ecpm,
	   income
	</sql>
	
	<select id="findByParams"  parameterType="map" resultType="com.cloudcross.ssp.model.TypeReport">
	    select 
	    a.id,device,100*(sum(impression)/(select sum(impression) from t_rpt_zone_device a, t_operator b
		where a.operator_id=b.id 
		<if test="operatorId!=null">
		    and a.operator_id=#{operatorId}
		</if> 
		   )) as pageviewsPercent,
		sum(pv) as pv,sum(uv) as uv,sum(impression) as impression,100*(sum(impression)/sum(pv)) as fillRate,
        sum(click) as click,100*(sum(click)/sum(impression)) as clickRate,sum(ecpm) as ecpm,sum(income) as income
		from t_rpt_zone_device a, t_operator b
		where a.operator_id=b.id 
		<if test="operatorId!=null">
		    and a.operator_id=#{operatorId}
		</if>
		group by device
		order by id asc 
		limit #{pager.offset}, #{pager.limit}    
	</select>
	
	<select id="countByParams" parameterType="map" resultType="int">
	    select count(a.id)
	    from t_rpt_zone_device a, t_operator b
		where a.operator_id=b.id 
		<if test="operatorId!=null">
		    and a.operator_id=#{operatorId}
		</if>
	</select>
	

</mapper>