<?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="io.renren.modules.app.dao.XqxxSelectDao">
	<select id="selectXqxx" parameterType="io.renren.modules.app.entity.XqxxSelectEntity"
		resultType="io.renren.modules.app.entity.XqxxSelectEntity">
		select t.*,XQXX.XQMC,sfdw.dwmc,(gr+dw+gg-zx) ye from
		(select XM.Xqbm,xm.xmbm,xm.xmmc,zhxx.dwbm,
		sum(case when ywpz.djlx='01' or ywpz.djlx='02' then ywpzmx.je else 0 end) dw,
		sum(case when ywpz.djlx='03' then ywpzmx.je else 0 end) gr,
		sum(case when ywpz.djlx='04' then ywpzmx.je else 0 end) gg,
		sum(case when ywpz.djlx='21' or ywpz.djlx='22' or ywpz.djlx='23' then ywpzmx.je
		else 0 end) zx
		from ywpz,ywpzmx,ZHXX,XM
		where ywpz.djbh = ywpzmx.djbh
		and ywpzmx.DHBM = ZHXX.Dhbm
		and ZHXX.Xmbm = XM.Xmbm
		and (XM.Xqbm = #{xqbm} or #{xqbm} is null)
		<!-- and XM.Gzdbm = #{gzdbm} -->
		and (zhxx.dwbm = #{dwbm} or #{dwbm} is null)
		group by XM.Xqbm,xm.xmbm,xm.xmmc,zhxx.dwbm) t
		left join XQXX on t.Xqbm=XQXX.XQBM
		left join SFDW on t.dwbm=SFDW.Dwbm
		order by XQXX.XQMC
	</select>

</mapper>