Browse Source

居民排行sql语句修改

dev
yujintao 6 years ago
parent
commit
471ea1718c
  1. 110
      esua-epdc/epdc-module/epdc-analysis/epdc-analysis-server/src/main/resources/mapper/user/UserAnalysisDao.xml

110
esua-epdc/epdc-module/epdc-analysis/epdc-analysis-server/src/main/resources/mapper/user/UserAnalysisDao.xml

@ -5,82 +5,66 @@
<select id="selectListUserDataRank" resultType="com.elink.esua.epdc.dto.user.result.UserDataRankResultDTO">
SELECT
t1.*,
t1.userCount - t1.partyMemberCount AS residentCount,
t1.oldCount / t1.userCount oldPercent,
t1.youngCount / t1.userCount youngPercent
FROM
( SELECT
t.streetName, t.streetId,
COUNT( t.userId ) userCount,
count( t.SEX = '1' OR NULL ) maleCount,
count( t.SEX = '0' OR NULL ) femaleCount,
count( t.SEX = '2' OR NULL ) unknownSexCount,
COUNT( t.PARTY_FLAG = '1' OR NULL ) partyMemberCount,
COUNT( t.age > 50 OR NULL ) oldCount,
COUNT( ( t.age &lt;= 50 AND t.age > 0 ) OR NULL ) youngCount
t1.*,
t1.userCount - t1.partyMemberCount AS residentCount,
t1.oldCount / t1.userCount oldPercent,
t1.youngCount / t1.userCount youngPercent
FROM (
SELECT
uu.ID AS userId,
CASE WHEN uu.SEX != '0' AND uu.SEX != '1' THEN '2' ELSE uu.SEX END AS SEX,
uu.STATE, uu.PARTY_FLAG, uu.DEPT_ID,
IFNULL( TIMESTAMPDIFF( YEAR, uu.BIRTHDAY, CURDATE()), 0 ) age,
ad2.`name` streetName, ad2.id streetId
FROM
esua_epdc_user.epdc_user uu
LEFT JOIN esua_epdc_admin.sys_dept ad ON uu.DEPT_ID = ad.ID AND ad.type_key = 'grid_party'
LEFT JOIN esua_epdc_admin.sys_dept ad1 ON ad.pid = ad1.id
LEFT JOIN esua_epdc_admin.sys_dept ad2 ON ad1.pid = ad2.id
WHERE uu.DEL_FLAG = '0'
<if test='partyFlag != null and partyFlag = "1"'>AND uu.PARTY_FLAG = '1'</if>
<choose>
<when test="gridId != null and gridId != ''">AND ad2.ID = #{gridId}</when>
<otherwise>
<if test="deptIdList != null and deptIdList.size() > 0">
AND ad2.ID in
<foreach collection="deptIdList" item="deptId" open="(" separator="," close=")">#{deptId}</foreach>
</if>
</otherwise>
</choose>
/*待补充按时间筛选*/
<if test="startTime != null and endTime != null and endTime != ''">
AND uu.CREATED_TIME BETWEEN #{startTime}
AND #{endTime}
</if>
AND ad.id IS NOT NULL
) t
SELECT
t.streetName, t.streetId,
COUNT( t.userId ) userCount,
count( t.SEX = '1' OR NULL ) maleCount,
count( t.SEX = '0' OR NULL ) femaleCount,
count( t.SEX = '2' OR NULL ) unknownSexCount,
COUNT( t.PARTY_FLAG = '1' OR NULL ) partyMemberCount,
COUNT( t.age > 50 OR NULL ) oldCount,
COUNT( ( t.age &lt;= 50 AND t.age > 0 ) OR NULL ) youngCount
FROM (
SELECT
uu.ID AS userId,
CASE WHEN uu.SEX != '0' AND uu.SEX != '1' THEN '2' ELSE uu.SEX END AS SEX,
uu.STATE, uu.PARTY_FLAG, uu.DEPT_ID,
IFNULL( TIMESTAMPDIFF( YEAR, uu.BIRTHDAY, CURDATE()), 0 ) age,
ad2.`name` streetName, ad2.id streetId
FROM
esua_epdc_user.epdc_user uu
LEFT JOIN esua_epdc_admin.sys_dept ad ON uu.DEPT_ID = ad.ID AND ad.type_key = 'grid_party'
LEFT JOIN esua_epdc_admin.sys_dept ad1 ON ad.pid = ad1.id
LEFT JOIN esua_epdc_admin.sys_dept ad2 ON ad1.pid = ad2.id
WHERE uu.DEL_FLAG = '0'
<if test='partyFlag != null and partyFlag = "1"'>AND uu.PARTY_FLAG = '1'</if>
<choose>
<when test="gridId != null and gridId != ''">AND ad2.ID = #{gridId}</when>
<otherwise>
<if test="deptIdList != null and deptIdList.size() > 0">
AND ad2.ID in
<foreach collection="deptIdList" item="deptId" open="(" separator="," close=")">#{deptId}</foreach>
</if>
</otherwise>
</choose>
<if test="startTime != null and endTime != null and endTime != ''">
AND uu.CREATED_TIME BETWEEN #{startTime} AND #{endTime}
</if>
AND ad.id IS NOT NULL
) t
) t1
<choose>
<when test='category != null and category == "0"'>
where
t.age>=50
GROUP BY t.streetId
order by COUNT( t.age > 50 OR NULL ) desc
order by t1.oldCount desc
</when>
<when test='category != null and category == "1"'>
where
t.age &lt; 50
GROUP BY t.streetId
order by COUNT( ( t.age &lt;= 50 AND t.age > 0 ) OR NULL ) desc
order by t1.youngCount desc
</when>
<when test='category != null and category == "2"'>
where
t.age>=50
GROUP BY t.streetId
order by COUNT( t.age > 50 OR NULL ) asc
order by t1.oldCount asc
</when>
<when test='category != null and category == "3"'>
where
t.age &lt; 50
GROUP BY t.streetId
order by COUNT( t.age > 50 OR NULL ) asc
order by t1.youngCount asc
</when>
<otherwise>
GROUP BY t.streetId
GROUP BY t1.streetId
</otherwise>
</choose>
) t1
/*待补充排序功能 order by t1.oldCount asc */
</select>
<select id="selectListPartyAuthFailureCountByStreetId"

Loading…
Cancel
Save