SELECT 
e.*
<#if (userEntity.attType)?? && userEntity.attType != 0>
,(
	SELECT earlymonth_total 
	from t_bus_dept_user_statistics pus 
	WHERE pus.pjt_id=e.pid and pus.dept_id = e.deptId
	and pus.yearmonth=e.minyearmonth
) earlyMonthTotal
,(
	SELECT endmonth_total 
	from t_bus_dept_user_statistics pus 
	WHERE pus.pjt_id=e.pid and pus.dept_id = e.deptId
	and pus.yearmonth=e.maxyearmonth
) endMonthTotal
</#if>
FROM
	(
		SELECT u.pjt_id pid,d.description pName,u.dept_id deptId,d1.departname pidName,u.yearmonth
		<#if (userEntity.attType)?? && userEntity.attType != 0>
			,MAX(yearmonth) maxyearmonth,MIN(yearmonth) minyearmonth
			,sum(u.entry_total) entryTotal,sum(u.leave_total) leaveTotal
			,convert(avg(u.flow_proportion),decimal(10,2)) flowProportion
		</#if>
		<#if (userEntity.attType)?? && userEntity.attType == 0>
			,u.earlymonth_total earlyMonthTotal ,u.endmonth_total endMonthTotal
			,u.entry_total entryTotal,u.leave_total leaveTotal
			,u.flow_proportion flowProportion
		</#if>
		from t_bus_dept_user_statistics u
		LEFT JOIN t_s_depart d
		ON u.pjt_id = d.ID
		LEFT JOIN t_s_depart d1
		ON u.dept_id = d1.ID
		where 1=1
		<#if (userEntity.attType)?? && userEntity.attType == 2>
			and u.yearmonth like CONCAT('%', :userEntity.attYear ,'%') 
		</#if>
		<#if (userEntity.attType)?? && userEntity.attType != 2>
			<#if ( userEntity.yearMonthList )?? && userEntity.yearMonthList ?size gt 0>
					and u.yearmonth in(
					<#list userEntity.yearMonthList as item>        
					   '${item}'
					 <#if item_has_next>,</#if>
					 </#list>
					)
			</#if>
		</#if>
		<#if (userEntity.pid)??>
				 and u.pjt_id =:userEntity.pid
		</#if>
		GROUP BY u.dept_id
) e
order by flowProportion desc