复杂SQL示例 (排行榜需求)
Posted ltmei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了复杂SQL示例 (排行榜需求)相关的知识,希望对你有一定的参考价值。
这是公司的一个排行榜的需求,这期SQL记下来,方便以后查看
<?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="com.lieni.tuimu.dim.ranking.mapper.DimTuimuRankingListMapper"> <!-- 共用条件时间筛选条件 --> <sql id="reward_time"> <if test="startTime != null">AND payment.RECEIVED_PAYMENTS_TIME > #{startTime}</if> <if test="endTime != null">AND payment.RECEIVED_PAYMENTS_TIME < #{endTime}</if> </sql> <sql id="entery_time"> <if test="startTime != null">AND placement.ENTER_OPERATOR_TIME > #{startTime}</if> <if test="endTime != null">AND placement.ENTER_OPERATOR_TIME < #{endTime}</if> </sql> <sql id="pament_time"> <if test="startTime != null and endTime != null"> AND IF ( recommend.CHARGE_TYPE = 1, interview.CREATE_TIME > #{startTime} AND interview.CREATE_TIME < #{endTime}, IF(recommend.CHARGE_TYPE = 2, placement.CREATE_TIME > #{startTime} AND placement.CREATE_TIME < #{endTime},NULL) ) </if> </sql> <sql id="interview_time"> <if test="startTime != null">AND interview.INTERVIEW_TIME > #{startTime}</if> <if test="endTime != null">AND interview.INTERVIEW_TIME < #{endTime}</if> </sql> <sql id="placemengt_time"> <if test="startTime != null">AND placement.CREATE_TIME > #{startTime}</if> <if test="endTime != null">AND placement.CREATE_TIME < #{endTime}</if> </sql> <sql id="signed_time"> <if test="startTime != null">AND company.SIGN_TIME > #{startTime}</if> <if test="endTime != null">AND company.SIGN_TIME < #{endTime}</if> </sql> <!-- 公共排序条件 --> <sql id="commond_order_by"> <if test="sortType != null and sortType > 0"> <if test="sortType == 1"> ORDER BY reward.rewardTotal DESC, pament.pamentTotal DESC, entery.enteryTotal DESC, placement.offerCountTotal DESC,interviewed.interviewedTotal DESC,signed.signTotal DESC </if> <if test="sortType == 2"> ORDER BY pament.pamentTotal DESC, placement.offerCountTotal DESC, interviewed.interviewedTotal DESC, reward.rewardTotal DESC, signed.signTotal DESC, entery.enteryTotal DESC </if> <if test="sortType == 3"> ORDER BY entery.enteryTotal DESC, pament.pamentTotal DESC, placement.offerCountTotal DESC, interviewed.interviewedTotal DESC, reward.rewardTotal DESC, signed.signTotal DESC </if> <if test="sortType == 4"> ORDER BY interviewed.interviewedTotal DESC, entery.enteryTotal DESC, pament.pamentTotal DESC, placement.offerCountTotal DESC,reward.rewardTotal DESC, signed.signTotal DESC </if> <!-- 按组排行的条件 --> <if test="sortType == 6"> ORDER BY reward.rewardTotal DESC, pament.pamentTotal DESC, signed.signTotal DESC, entery.enteryTotal DESC,placement.offerCountTotal DESC,interviewed.interviewedTotal DESC </if> </if> </sql> <!-- AM个人排行榜(不包括签约王) --> <select id="selectBySalesAdmin" resultType="com.lieni.tuimu.dim.model.ExtraRankingList"> SELECT staff.`ID` AS ‘amId‘, staff.`NAME` AS ‘amName‘, reward.rewardTotal AS ‘rewardTotal‘, -- 已回款总数 placement.offerCountTotal AS ‘offerCountTotal‘, -- offer数量 interviewed.interviewedTotal AS ‘interviewedTotal‘,-- 到面数 entery.enteryTotal AS ‘enteryTotal‘,-- 入职数 pament.pamentTotal AS ‘pamentTotal‘, -- 佣金数 signed.signTotal AS ‘signTotal‘ -- 签约数 FROM T_DIM_TUIMU_STAFF staff LEFT JOIN ( SELECT IFNULL(SUM(payment.RECEIVED_PAYMENTS_AMOUNT),0) AS rewardTotal, -- 已回款总数 payment.SALES_ADMIN AS SALES_ADMIN FROM T_DIM_TUIMU_PAYMENT payment LEFT JOIN T_DIM_TUIMU_COMPANY company ON payment.COMPANY_ID = company.ID WHERE 1 = 1 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid="reward_time" /> GROUP BY payment.SALES_ADMIN) reward ON staff.ID = reward.SALES_ADMIN LEFT JOIN ( SELECT COUNT(1) AS enteryTotal, -- 入职数 recommendStream.SALES_ADMIN AS SALES_ADMIN FROM T_DIM_TUIMU_RECOMMEND_STREAM recommendStream LEFT JOIN T_DIM_TUIMU_PLACEMENT placement ON recommendStream.RECOMMEND_ID = placement.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON placement.COMPANY_ID = company.ID WHERE recommendStream.SOURCE_MAIN_STATUS = 6 AND recommendStream.SOURCE_CHILD_STATUS = 1 AND recommendStream.TARGET_MAIN_STATUS = 6 AND recommendStream.TARGET_CHILD_STATUS = 3 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid="entery_time" /> GROUP BY recommendStream.SALES_ADMIN) entery ON staff.ID = entery.SALES_ADMIN LEFT JOIN ( SELECT IFNULL(SUM(recommend.REWARD_ALL),0) AS pamentTotal, -- offer加推荐的总佣金 recommendStream.SALES_ADMIN AS SALES_ADMIN FROM T_DIM_TUIMU_RECOMMEND_STREAM recommendStream LEFT JOIN T_DIM_TUIMU_RECOMMEND recommend ON recommendStream.RECOMMEND_ID = recommend.ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON recommend.COMPANY_ID = company.ID LEFT JOIN T_DIM_TUIMU_PLACEMENT placement ON recommendStream.RECOMMEND_ID = placement.RECOMMEND_ID AND recommendStream.TARGET_MAIN_STATUS = 6 AND recommendStream.TARGET_CHILD_STATUS = 1 LEFT JOIN T_DIM_TUIMU_INTERVIEW interview ON recommendStream.RECOMMEND_ID = interview.RECOMMEND_ID AND recommendStream.TARGET_MAIN_STATUS = 5 AND recommendStream.TARGET_CHILD_STATUS = 1 AND interview.INTERVIEW_ROUND = 1 WHERE 1 = 1 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid="pament_time" /> GROUP BY recommendStream.SALES_ADMIN) pament ON staff.ID = pament.SALES_ADMIN LEFT JOIN ( SELECT COUNT(1) AS interviewedTotal, -- 到面数 recommendStream.SALES_ADMIN FROM T_DIM_TUIMU_RECOMMEND_STREAM recommendStream LEFT JOIN T_DIM_TUIMU_INTERVIEW interview ON recommendStream.RECOMMEND_ID = interview.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON interview.COMPANY_ID = company.ID WHERE (recommendStream.SOURCE_MAIN_STATUS = 5 AND recommendStream.SOURCE_CHILD_STATUS = 1 AND recommendStream.TARGET_MAIN_STATUS = 5 AND recommendStream.TARGET_CHILD_STATUS = 5) OR (recommendStream.SOURCE_MAIN_STATUS = 5 AND recommendStream.TARGET_MAIN_STATUS = 5 AND interview.INTERVIEW_ROUND = 2) <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid="interview_time" /> GROUP BY recommendStream.SALES_ADMIN) interviewed ON staff.ID = interviewed.SALES_ADMIN LEFT JOIN ( SELECT COUNT(1) AS offerCountTotal, -- offer 数量 recommendStream.SALES_ADMIN AS SALES_ADMIN FROM T_DIM_TUIMU_RECOMMEND_STREAM recommendStream LEFT JOIN T_DIM_TUIMU_PLACEMENT placement ON recommendStream.RECOMMEND_ID = placement.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON placement.COMPANY_ID = company.ID WHERE recommendStream.SOURCE_MAIN_STATUS = 6 AND recommendStream.SOURCE_CHILD_STATUS = 1 AND TARGET_MAIN_STATUS = 6 AND TARGET_CHILD_STATUS = 3 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid="placemengt_time" /> GROUP BY recommendStream.SALES_ADMIN) placement ON staff.ID = placement.SALES_ADMIN LEFT JOIN ( SELECT COUNT(1) AS signTotal, customerStream.SALES_ADMIN AS SALES_ADMIN FROM T_DIM_TUIMU_CUSTOMER_STREAM customerStream LEFT JOIN T_DIM_TUIMU_COMPANY company ON customerStream.CUSTOMER_ID = company.ID WHERE customerStream.SOURCE_SIGNED = 0 AND customerStream.TARGET_SIGNED = 1 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid="signed_time" /> GROUP BY customerStream.SALES_ADMIN) signed ON staff.ID = signed.SALES_ADMIN WHERE FIND_IN_SET(1,staff.DUTIES) <if test="roles != null and roles.size() >0"> AND staff.ROLE_ID IN <foreach collection="roles" item="role" open="(" close=")" separator=","> #{role} </foreach> </if> <if test="salesAdminIds != null and salesAdminIds.size() > 0"> AND staff.`ID` IN <foreach collection="salesAdminIds" item="userId" open="(" close=")" separator=","> #{userId} </foreach> </if> <include refid="commond_order_by"/> LIMIT 0, #{maxRows}; </select> <!-- 签约王 --> <select id="selectSignedContract" resultType="com.lieni.tuimu.dim.model.ExtraRankingList"> SELECT signed.signTotal AS signTotal, staff.ID AS amId, staff.`NAME` AS amName, companyInfo.companyName AS companyName, companyInfo.signTime AS signTime FROM T_DIM_TUIMU_STAFF staff LEFT JOIN ( SELECT COUNT(1) AS signTotal, customerStream.SALES_ADMIN AS SALES_ADMIN FROM T_DIM_TUIMU_CUSTOMER_STREAM customerStream LEFT JOIN T_DIM_TUIMU_COMPANY company ON customerStream.CUSTOMER_ID = company.ID WHERE customerStream.SOURCE_SIGNED = 0 AND customerStream.TARGET_SIGNED = 1 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid="signed_time" /> GROUP BY customerStream.SALES_ADMIN ) signed ON staff.ID = signed.SALES_ADMIN LEFT JOIN ( SELECT company.`NAME` AS companyName, customerStream.SALES_ADMIN AS SALES_ADMIN, company.SIGN_TIME AS signTime, company.TYPE AS companyType FROM T_DIM_TUIMU_CUSTOMER_STREAM customerStream LEFT JOIN T_DIM_TUIMU_COMPANY company ON customerStream.CUSTOMER_ID = company.ID WHERE customerStream.SOURCE_SIGNED = 0 AND customerStream.TARGET_SIGNED = 1 <include refid="signed_time" /> ) companyInfo ON companyInfo.SALES_ADMIN = staff.ID WHERE FIND_IN_SET(3,staff.DUTIES) AND staff.ROLE_ID IN (1, 2, 5, 6, 7, 8, 9) <if test="salesAdminIds != null and salesAdminIds.size() > 0"> AND staff.`ID` IN <foreach collection="salesAdminIds" item="userId" open="(" close=")" separator=","> #{userId} </foreach> </if> ORDER BY signed.signTotal,companyInfo.signTime LIMIT 0, #{maxRows}; </select> <!-- 指定组排行榜 --> <select id="selectByOrganization" resultType="com.lieni.tuimu.dim.model.ExtraRankingList"> SELECT organization.`ID` AS ‘groupId‘, organization.`NAME` AS ‘groupName‘, IFNULL(reward.rewardTotal, 0) AS ‘rewardTotal‘, IFNULL(pament.pamentTotal,0) AS ‘pamentTotal‘, IFNULL(signed.signTotal, 0) AS ‘signTotal‘, IFNULL(entery.enteryTotal, 0) AS ‘enteryTotal‘, IFNULL(placement.offerCountTotal, 0) AS ‘offerCountTotal‘, IFNULL(interviewed.interviewedTotal, 0) AS ‘interviewedTotal‘ FROM `T_DIM_TUIMU_ORGANIZATION` organization LEFT JOIN ( <!-- 按组的已回款总数 --> SELECT IFNULL(SUM(payment.RECEIVED_PAYMENTS_AMOUNT),0) AS ‘rewardTotal‘, <if test="organizations != null and organizations.size() > 0"> CASE <foreach collection="organizations" item="organization"> WHEN SUBSTRING_INDEX(payment.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> ELSE 0 END AS ‘organizationId‘ </if> <if test="organizations == null or organizations.size() <= 0"> SUBSTRING_INDEX(payment.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) AS ‘organizationId‘ </if> FROM T_DIM_TUIMU_PAYMENT payment LEFT JOIN T_DIM_TUIMU_COMPANY company ON payment.COMPANY_ID = company.ID WHERE 1 = 1 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid = "reward_time" /> GROUP BY payment.SALES_ADMIN_ORGANIZATION ) reward ON organization.`ID` = reward.`organizationId` LEFT JOIN ( <!-- offer加推荐的总佣金 --> SELECT SUM(payment_temp.rewardAll) AS pamentTotal, payment_temp.organizationId FROM ( SELECT recommend.REWARD_ALL AS rewardAll, <if test="organizations != null and organizations.size() > 0"> CASE <foreach collection="organizations" item="organization"> WHEN SUBSTRING_INDEX(recommendStream.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> ELSE 0 END AS ‘organizationId‘ </if> <if test="organizations == null or organizations.size() <= 0"> SUBSTRING_INDEX(recommendStream.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) AS ‘organizationId‘ </if> FROM T_DIM_TUIMU_RECOMMEND_STREAM recommendStream LEFT JOIN T_DIM_TUIMU_RECOMMEND recommend ON recommendStream.RECOMMEND_ID = recommend.ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON recommend.COMPANY_ID = company.ID LEFT JOIN T_DIM_TUIMU_PLACEMENT placement ON recommendStream.RECOMMEND_ID = placement.RECOMMEND_ID AND recommendStream.TARGET_MAIN_STATUS = 6 AND recommendStream.TARGET_CHILD_STATUS = 1 LEFT JOIN T_DIM_TUIMU_INTERVIEW interview ON recommendStream.RECOMMEND_ID = interview.RECOMMEND_ID AND recommendStream.TARGET_MAIN_STATUS = 5 AND recommendStream.TARGET_CHILD_STATUS = 1 AND interview.INTERVIEW_ROUND = 1 WHERE 1 = 1 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid = "pament_time" /> ) payment_temp GROUP BY payment_temp.organizationId ) pament ON organization.ID = pament.organizationId LEFT JOIN ( <!-- 签约数 --> SELECT COUNT(DISTINCT sign_temp.companyId) AS signTotal, sign_temp.organizationId FROM ( SELECT company.ID AS companyId, <if test="organizations != null and organizations.size() > 0"> CASE <foreach collection="organizations" item="organization"> WHEN SUBSTRING_INDEX(customerStream.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> ELSE 0 END AS ‘organizationId‘ </if> <if test="organizations == null or organizations.size() <= 0"> SUBSTRING_INDEX(customerStream.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) AS ‘organizationId‘ </if> FROM T_DIM_TUIMU_CUSTOMER_STREAM customerStream LEFT JOIN T_DIM_TUIMU_COMPANY company ON customerStream.CUSTOMER_ID = company.ID WHERE customerStream.SOURCE_SIGNED = 0 AND customerStream.TARGET_SIGNED = 1 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid = "signed_time" /> ) sign_temp GROUP BY sign_temp.organizationId ) signed ON organization.ID = signed.organizationId LEFT JOIN ( <!-- 入职数 --> SELECT COUNT(DISTINCT entery_temp.placementId) AS enteryTotal, entery_temp.organizationId FROM ( SELECT placement.ID AS placementId, <if test="organizations != null and organizations.size() > 0"> CASE <foreach collection="organizations" item="organization"> WHEN SUBSTRING_INDEX(recommendStream.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> ELSE 0 END AS ‘organizationId‘ </if> <if test="organizations == null or organizations.size() <= 0"> SUBSTRING_INDEX(recommendStream.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) AS ‘organizationId‘ </if> FROM T_DIM_TUIMU_RECOMMEND_STREAM recommendStream LEFT JOIN T_DIM_TUIMU_PLACEMENT placement ON recommendStream.RECOMMEND_ID = placement.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON placement.COMPANY_ID = company.ID AND company.TYPE =0 WHERE recommendStream.SOURCE_MAIN_STATUS = 6 AND recommendStream.SOURCE_CHILD_STATUS = 1 AND recommendStream.TARGET_MAIN_STATUS = 6 AND recommendStream.TARGET_CHILD_STATUS = 3 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid="entery_time" /> ) entery_temp GROUP BY entery_temp.organizationId ) entery ON organization.ID = entery.organizationId LEFT JOIN ( <!-- offer数 --> SELECT COUNT(DISTINCT offer_temp.placementId) AS offerCountTotal, offer_temp.organizationId FROM ( SELECT placement.ID AS placementId, <if test="organizations != null and organizations.size() > 0"> CASE <foreach collection="organizations" item="organization"> WHEN SUBSTRING_INDEX(recommendStream.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> ELSE 0 END AS ‘organizationId‘ </if> <if test="organizations == null or organizations.size() <= 0"> SUBSTRING_INDEX(recommendStream.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) AS ‘organizationId‘ </if> FROM T_DIM_TUIMU_RECOMMEND_STREAM recommendStream LEFT JOIN T_DIM_TUIMU_PLACEMENT placement ON recommendStream.RECOMMEND_ID = placement.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON placement.COMPANY_ID = company.ID WHERE recommendStream.SOURCE_MAIN_STATUS = 6 AND recommendStream.SOURCE_CHILD_STATUS = 1 AND TARGET_MAIN_STATUS = 6 AND TARGET_CHILD_STATUS = 3 <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid="placemengt_time" /> ) offer_temp GROUP BY offer_temp.organizationId ) placement ON organization.ID = placement.organizationId LEFT JOIN ( <!-- 到面数 --> SELECT COUNT(DISTINCT interview_temp.interviewedId ) AS interviewedTotal, interview_temp.organizationId FROM ( SELECT interview.ID AS interviewedId, <if test="organizations != null and organizations.size() > 0"> CASE <foreach collection="organizations" item="organization"> WHEN SUBSTRING_INDEX(recommendStream.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> ELSE 0 END AS ‘organizationId‘ </if> <if test="organizations == null or organizations.size() <= 0"> SUBSTRING_INDEX(recommendStream.SALES_ADMIN_ORGANIZATION, ‘,‘ ,1) AS ‘organizationId‘ </if> FROM T_DIM_TUIMU_RECOMMEND_STREAM recommendStream LEFT JOIN T_DIM_TUIMU_INTERVIEW interview ON recommendStream.RECOMMEND_ID = interview.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON interview.COMPANY_ID = company.ID WHERE (recommendStream.SOURCE_MAIN_STATUS = 5 AND recommendStream.SOURCE_CHILD_STATUS = 1 AND recommendStream.TARGET_MAIN_STATUS = 5 AND recommendStream.TARGET_CHILD_STATUS = 5) OR (recommendStream.SOURCE_MAIN_STATUS = 5 AND recommendStream.TARGET_MAIN_STATUS = 5 AND interview.INTERVIEW_ROUND = 2) <if test="type != null and type > 0"> AND company.TYPE = #{type} </if> <include refid="interview_time" /> ) interview_temp GROUP BY interview_temp.organizationId ) interviewed ON organization.ID = interviewed.organizationId <where> 1 = 1 <if test="organizations != null and organizations.size() > 0"> AND organization.`ID` IN <foreach collection="organizations" item="organization" open="(" close=")" separator=","> #{organization.parentId} </foreach> </if> </where> <include refid="commond_order_by"/> LIMIT 0, #{maxRows}; </select> </mapper>
以上是关于复杂SQL示例 (排行榜需求)的主要内容,如果未能解决你的问题,请参考以下文章
安全测试 web安全测试 常规安全漏洞 可能存在SQL和JS注入漏洞场景分析。为什么自己没有找到漏洞,哪么可能存在漏洞场景是?SQL注入漏洞修复 JS注入漏洞修复 漏洞存在场景分析和修复示例(代码片段