with temp0 as ( select * from School where areaid = #{areaId} ) , temp1 as ( select a.schoolid , count( a.classesid ) as classcount from classes a where a.areaid = #{areaId} and a.status = 1 and a.classtype = 1 group by a.schoolid ) , temp3 as ( select uid from orderinfo where payway = 0 and isdeleted = 0 and created > #{startTime} and created < #{endTime} and areaid = #{areaId} ) , temp4 as ( select b.uid , max( b.endtime ) as endtime from UserOwnLesson b where b.areaid = #{areaId} and b.uid not in ( select uid from temp3 ) group by b.uid having max( b.endtime ) < now() having max( b.endtime ) > now() ) , temp2 as ( select a.schoolid , count( b.studentid ) as studentcount from classes a inner join StudentOwnClass b on a.status = 1 and a.areaid = #{areaId} and a.classesid = b.classesid and b.status = 1 and b.areaid = #{areaId} inner join temp4 c on b.studentid = c.uid group by schoolid ) select a.schoolid as objectId , a.schoolname as objectName ,case when b.classcount is null then 0 else b.classcount end as numbercount , case when c.studentcount is null then 0 else c.studentcount end as payAmount from temp0 a left join temp1 b on a.schoolid = b.schoolid left join temp2 c on a.schoolid = c.schoolid order by a.schoolid limit #{pageSize} offset #{offset} with temp0 as ( select * from Area where parentid = #{areaId} ) , temp5 as (select a.parentid, count(a.areaid) as areaCount from area as a inner join temp0 as b on a.parentid = b.areaid group by a.parentid ), temp1 as ( select ${floor_str} as areaid , count(*) as schoolcount from School a where a.status = 1 and a.areaid > #{startArea} and a.areaid < #{endArea} group by ${floor_str} ), temp3 as ( select uid from orderinfo where payway > 0 and ispayed = 1 and isdeleted = 0 and created > #{startTime} and created < #{endTime} and areaid > #{startArea} and areaid < #{endArea} ) , temp4 as ( select b.uid , max( b.endtime ) as endtime from UserOwnLesson b inner join StudentOwnClass so on b.uid = so.studentid and so.status = 1 inner join Classes c on c.classesId = so.classesId and c.classtype =1 and c.status= 1 inner join School sc on c.schoolId = sc.schoolId and sc.status=1 where b.areaid > #{startArea} and b.areaid < #{endArea} and b.uid not in ( select uid from temp3 ) group by b.uid having max( b.endtime ) < now() having max( b.endtime ) > now() ) , temp2 as ( select ${floor_str} as areaid , count( a.studentid ) as studentcount from Student a inner join temp4 b on a.status = 1 and a.areaid > #{startArea} and a.areaid < #{endArea} and a.studentid = b.uid group by ${floor_str} ) select a.areaid as objectId, a.areaname as objectName ,case when b.schoolcount is null then 0 else b.schoolcount end as numbercount , case when c.studentcount is null then 0 else c.studentcount end as payAmount from temp0 a left join temp1 b on a.areaid = b.areaid left join temp2 c on a.areaid = c.areaid order by a.areaid limit #{pageSize} offset #{offset} select a.areaid as objectId, a.areaname as objectName ,case when d.areaCount is null then 0 else d.areaCount end as numberCount, case when c.studentcount is null then 0 else c.studentcount end as payAmount from temp0 a left join temp2 c on a.areaid = c.areaid left join temp5 d on d.parentid = a.areaid order by a.areaid limit #{pageSize} offset #{offset}