复杂SQL示例 (排行榜需求)
公司项目要求做出排行榜,根据六组数据依次排行,关联多表,SQL记录下来方便日后查看
" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http:-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 placement.CREATE_TIME > #{startTime}</if> <if test="endTime != null">AND placement.CREATE_TIME < #{endTime}</if> </sql> <sql id="first_interview_time"> <if test="startTime != null">AND interview.CREATE_TIME > #{startTime}</if> <if test="endTime != null">AND interview.CREATE_TIME < #{endTime}</if> </sql> <sql id="interview_time"> <if test="startTime != null">AND recommendStream.CREATE_TIME > #{startTime}</if> <if test="endTime != null">AND recommendStream.CREATE_TIME < #{endTime}</if> </sql> <sql id="placement_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"> "> "> ORDER BY reward.rewardTotal DESC, pament.pamentTotal DESC, entery.enteryTotal DESC, placement.offerCountTotal DESC,interviewed.interviewedTotal DESC,signed.signTotal DESC </if> "> ORDER BY pament.pamentTotal DESC, placement.offerCountTotal DESC, interviewed.interviewedTotal DESC, reward.rewardTotal DESC, signed.signTotal DESC, entery.enteryTotal DESC </if> "> ORDER BY entery.enteryTotal DESC, pament.pamentTotal DESC, placement.offerCountTotal DESC, interviewed.interviewedTotal DESC, reward.rewardTotal DESC, signed.signTotal DESC </if> "> ORDER BY interviewed.interviewedTotal DESC, entery.enteryTotal DESC, pament.pamentTotal DESC, placement.offerCountTotal DESC,reward.rewardTotal DESC, signed.signTotal DESC </if> <!-- 按组排行的条件 --> "> 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()),) AS rewardTotal, -- 已回款总数 payment.HUNTER_ADMIN_ID AS HUNTER_ADMIN_ID FROM T_DIM_TUIMU_PAYMENT payment LEFT JOIN T_DIM_TUIMU_COMPANY company ON payment.COMPANY_ID = company.ID WHERE "> AND company.TYPE = #{type} </if> <include refid="reward_time" /> GROUP BY payment.HUNTER_ADMIN_ID) reward ON staff.ID = reward.HUNTER_ADMIN_ID LEFT JOIN ( SELECT ) AS enteryTotal, -- 入职数 recommendStream.HUNTER_ADMIN_ID AS HUNTER_ADMIN_ID 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.TARGET_MAIN_STATUS "> AND company.TYPE = #{type} </if> <include refid="entery_time" /> GROUP BY recommendStream.HUNTER_ADMIN_ID) entery ON staff.ID = entery.HUNTER_ADMIN_ID LEFT JOIN ( SELECT SUM(pament.pamentTotal) AS pamentTotal, pament.HUNTER_ADMIN_ID AS HUNTER_ADMIN_ID FROM -- 到面产品 ( SELECT IFNULL()),) AS pamentTotal, -- offer加推荐的总佣金 recommendStream.HUNTER_ADMIN_ID AS HUNTER_ADMIN_ID FROM T_DIM_TUIMU_RECOMMEND recommend LEFT JOIN T_DIM_TUIMU_RECOMMEND_STREAM recommendStream ON recommendStream.RECOMMEND_ID = recommend.ID LEFT JOIN T_DIM_TUIMU_INTERVIEW interview ON recommendStream.RECOMMEND_ID = interview.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON recommend.COMPANY_ID = company.ID WHERE "> AND company.TYPE = #{type} </if> <include refid="first_interview_time" /> GROUP BY recommendStream.HUNTER_ADMIN_ID UNION ALL -- 非到面产品 SELECT IFNULL()),) AS pamentTotal, recommendStream.HUNTER_ADMIN_ID AS HUNTER_ADMIN_ID FROM T_DIM_TUIMU_PLACEMENT placement LEFT JOIN T_DIM_TUIMU_RECOMMEND_STREAM recommendStream ON recommendStream.RECOMMEND_ID = placement.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_RECOMMEND recommend ON recommend.ID = recommendStream.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON recommend.COMPANY_ID = company.ID WHERE "> AND company.TYPE = #{type} </if> <include refid="pament_time" /> GROUP BY recommendStream.HUNTER_ADMIN_ID ) pament GROUP BY pament.HUNTER_ADMIN_ID) pament ON staff.ID = pament.HUNTER_ADMIN_ID LEFT JOIN ( SELECT ) AS interviewedTotal, recommendStream.HUNTER_ADMIN_ID AS HUNTER_ADMIN_ID FROM T_DIM_TUIMU_COMPANY company LEFT JOIN T_DIM_TUIMU_RECOMMEND_STREAM recommendStream ON recommendStream.COMPANY_ID = company.ID WHERE (recommendStream.SOURCE_MAIN_STATUS ,)) OR (recommendStream.SOURCE_MAIN_STATUS ) "> AND company.TYPE = #{type} </if> <include refid="interview_time" /> GROUP BY recommendStream.HUNTER_ADMIN_ID) interviewed ON staff.ID = interviewed.HUNTER_ADMIN_ID LEFT JOIN ( SELECT ) AS offerCountTotal, -- offer 数量 recommendStream.HUNTER_ADMIN_ID AS HUNTER_ADMIN_ID 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.TARGET_MAIN_STATUS "> AND company.TYPE = #{type} </if> <include refid="placement_time" /> GROUP BY recommendStream.HUNTER_ADMIN_ID) placement ON staff.ID = placement.HUNTER_ADMIN_ID LEFT JOIN ( SELECT ) AS signTotal, customerStream.SERVICE_ADMIN AS SERVICE_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 "> AND company.TYPE = #{type} </if> <include refid="signed_time" /> GROUP BY customerStream.SERVICE_ADMIN) signed ON staff.ID = signed.SERVICE_ADMIN WHERE FIND_IN_SET(,staff.DUTIES) "> AND staff.ROLE_ID IN <foreach collection="roles" item="role" open="(" close=")" separator=","> #{role} </foreach> </if> "> AND staff.`ID` IN <foreach collection="salesAdminIds" item="userId" open="(" close=")" separator=","> #{userId} </foreach> </if> <include refid="commond_order_by"/> LIMIT , #{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, signed.signTime AS signTime FROM T_DIM_TUIMU_STAFF staff LEFT JOIN ( SELECT ) AS signTotal, customerStream.SALES_ADMIN AS SALES_ADMIN, MAX(company.SIGN_TIME) AS signTime FROM T_DIM_TUIMU_COMPANY company LEFT JOIN T_DIM_TUIMU_CUSTOMER_STREAM customerStream ON customerStream.CUSTOMER_ID = company.ID WHERE customerStream.SOURCE_SIGNED <include refid="signed_time" /> GROUP BY customerStream.SALES_ADMIN ) signed ON signed.SALES_ADMIN = staff.ID WHERE FIND_IN_SET(,staff.DUTIES) , , , , , ) "> AND staff.`ID` IN <foreach collection="salesAdminIds" item="userId" open="(" close=")" separator=","> #{userId} </foreach> </if> ORDER BY signed.signTotal,signed.signTime LIMIT , #{maxRows}; </select> <!-- 指定组排行榜 --> <select id="selectByOrganization" resultType="com.lieni.tuimu.dim.model.ExtraRankingList"> SELECT organization.`ID` AS 'groupId', organization.`NAME` AS 'groupName', IFNULL(reward.rewardTotal, ) AS 'rewardTotal', IFNULL(pament.pamentTotal,) AS 'pamentTotal', IFNULL(signed.signTotal, ) AS 'signTotal', IFNULL(entery.enteryTotal, ) AS 'enteryTotal', IFNULL(placement.offerCountTotal, ) AS 'offerCountTotal', IFNULL(interviewed.interviewedTotal, ) AS 'interviewedTotal' FROM `T_DIM_TUIMU_ORGANIZATION` organization LEFT JOIN ( <!-- 按组的已回款总数 --> SELECT IFNULL()),) AS 'rewardTotal', "> CASE <foreach collection="organizations" item="organization"> ) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> END AS 'organizationId' </if> "> SUBSTRING_INDEX(payment.HUNTER_ADMIN_ORGANIZATION, ) AS 'organizationId' </if> FROM T_DIM_TUIMU_PAYMENT payment LEFT JOIN T_DIM_TUIMU_COMPANY company ON payment.COMPANY_ID = company.ID WHERE "> AND company.TYPE = #{type} </if> <include refid = "reward_time" /> GROUP BY payment.HUNTER_ADMIN_ORGANIZATION ) reward ON organization.`ID` = reward.`organizationId` LEFT JOIN ( <!-- offer加推荐的总佣金 --> SELECT )) AS pamentTotal, pament.organizationId FROM -- 到面产品 ( SELECT recommend.REWARD_ALL AS rewardAll, -- offer加推荐的总佣金 "> CASE <foreach collection="organizations" item="organization"> ) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> END AS 'organizationId' </if> "> SUBSTRING_INDEX(recommendStream.HUNTER_ADMIN_ORGANIZATION, ) AS 'organizationId' </if> FROM T_DIM_TUIMU_RECOMMEND recommend LEFT JOIN T_DIM_TUIMU_RECOMMEND_STREAM recommendStream ON recommendStream.RECOMMEND_ID = recommend.ID LEFT JOIN T_DIM_TUIMU_INTERVIEW interview ON recommendStream.RECOMMEND_ID = interview.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON recommend.COMPANY_ID = company.ID WHERE "> AND company.TYPE = #{type} </if> <include refid="first_interview_time" /> UNION ALL -- 非到面产品 SELECT recommend.REWARD_ALL AS rewardAll, "> CASE <foreach collection="organizations" item="organization"> ) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> END AS 'organizationId' </if> "> SUBSTRING_INDEX(recommendStream.HUNTER_ADMIN_ORGANIZATION, ) AS 'organizationId' </if> FROM T_DIM_TUIMU_PLACEMENT placement LEFT JOIN T_DIM_TUIMU_RECOMMEND_STREAM recommendStream ON recommendStream.RECOMMEND_ID = placement.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_RECOMMEND recommend ON recommend.ID = recommendStream.RECOMMEND_ID LEFT JOIN T_DIM_TUIMU_COMPANY company ON recommend.COMPANY_ID = company.ID WHERE "> AND company.TYPE = #{type} </if> <include refid="pament_time" /> ) pament GROUP BY pament.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, "> CASE <foreach collection="organizations" item="organization"> ) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> END AS 'organizationId' </if> "> SUBSTRING_INDEX(customerStream.SALES_ADMIN_ORGANIZATION, ) 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 "> 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, "> CASE <foreach collection="organizations" item="organization"> ) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> END AS 'organizationId' </if> "> SUBSTRING_INDEX(recommendStream.HUNTER_ADMIN_ORGANIZATION, ) 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 "> 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, "> CASE <foreach collection="organizations" item="organization"> ) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> END AS 'organizationId' </if> "> SUBSTRING_INDEX(recommendStream.HUNTER_ADMIN_ORGANIZATION, ) 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 TARGET_MAIN_STATUS "> AND company.TYPE = #{type} </if> <include refid="placement_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 recommendStream.RECOMMEND_ID AS interviewedId, "> CASE <foreach collection="organizations" item="organization"> ) IN <foreach collection="organization.children" item="child" open="(" close=")" separator=","> #{child} </foreach> THEN #{organization.parentId} </foreach> END AS 'organizationId' </if> "> SUBSTRING_INDEX(recommendStream.HUNTER_ADMIN_ORGANIZATION, ) AS 'organizationId' </if> FROM T_DIM_TUIMU_COMPANY company LEFT JOIN T_DIM_TUIMU_RECOMMEND_STREAM recommendStream ON recommendStream.COMPANY_ID = company.ID WHERE (recommendStream.SOURCE_MAIN_STATUS ,)) OR (recommendStream.SOURCE_MAIN_STATUS ) "> AND company.TYPE = #{type} </if> <include refid="interview_time" /> ) interview_temp GROUP BY interview_temp.organizationId ) interviewed ON organization.ID = interviewed.organizationId <where> "> AND organization.`ID` IN <foreach collection="organizations" item="organization" open="(" close=")" separator=","> #{organization.parentId} </foreach> </if> </where> <include refid="commond_order_by"/> LIMIT , #{maxRows}; </select> </mapper>
复杂SQL示例 (排行榜需求)的更多相关文章
- springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目
一个简单的用户登录系统 用户有账号密码,登录ip,登录时间 打开登录页面,输入用户名密码 登录日志,可以记录登陆的时间,登陆的ip 成功登陆了的话,就更新用户的最后登入时间和ip,同时记录一条登录记录 ...
- Sql示例说明如何分组后求中间值--【叶子】
原文:Sql示例说明如何分组后求中间值--[叶子] 这里所谓的分组后求中间值是个什么概念呢? 我举个例子来说明一下: 假设我们现在有下面这样一个表: type name price -- ...
- SQL Server中Table字典数据的查询SQL示例代码
SQL Server中Table字典数据的查询SQL示例代码 前言 在数据库系统原理与设计(第3版)教科书中这样写道: 数据库包含4类数据: 1.用户数据 2.元数据 3.索引 4.应用元数据 其中, ...
- 如何将一个本来需要发几十条sql语句的需求优化成两条(非sql语句优化,而是解决方法)
首先声明,本人所有博客均为原创,谢绝转载! 今天接到一个需求,如下 这里解释一下,为什么是几十条,因为这里面每个公式的每个条件都是一个单独的数据如净利润就是一条sql语句,而且分散在不同的表,如果此方 ...
- 通过存储过程进行分页查询的SQL示例
--创建人:zengfanlong --创建时间:-- :: --说明:根据公司简写代码获取当前待同步的气瓶档案数据(分页获取) ALTER PROCEDURE [UP_GasBottles_GetS ...
- 分组求和SQL示例
1.ROLLUP和CUBE函数,自动汇总数据 select * from test_tbl的数据这样的 col_a col_b col_c ---- ----- ...
- 转载:在做datatable时候查询数据和条数只用一次sql就可以解决需求
前言:最近用datatable处理数据比较多,所以在使用时候想提升性能 select * from t_hr_leave SELECT FOUND_ROWS() //返回查询记录的总数 select ...
- 一个列转行SQL示例(wm_concat函数和replace函数合用)
准备测试数据: create table test01( groupid number, a number, b number, c ...
- Egret5.2.2 微信小游戏行的示例排行榜
Egret5.2.2版本发布微信小游戏后,在开放数据域有一个默认排行榜.这个文件夹代码+图大小就22kb. 排行榜的效果就是示范用的,很丑...带翻页. 代码如下,基本就是使用canvas渲染了一个排 ...
随机推荐
- Crontab 入门
参考网址: http://www.centoscn.com/CentOS/help/2014/0820/3524.html 简单命令 service crond restart //重启crontab ...
- 玩转Spring Boot 自定义配置、导入XML配置与外部化配置
玩转Spring Boot 自定义配置.导入XML配置与外部化配置 在这里我会全面介绍在Spring Boot里面如何自定义配置,更改Spring Boot默认的配置,以及介绍各配置的优先 ...
- JavaBean与Map的相互转换
package com.bizvane.utils.tools; import java.lang.reflect.Field; import java.util.Arrays; import jav ...
- JAVA ——int 类型除法保留两位小数
@Test public void txfloat() { // TODO 自动生成的方法存根 int a=9; int b=7; DecimalFormat df=new DecimalFormat ...
- 集训队日常训练20180525-DIV1
A.2805 N*M的图,每次浇水(X1,Y1)-(X2,Y2)围成的矩形,问最后有多少点被浇水了. 暴力. #include<bits/stdc++.h> using namespace ...
- 模拟20 题解(waiting)
留坑待填 T2 #include<cstdio> #include<vector> #include<cstring> #include<iostream&g ...
- 【每日一linux命令7】用户及用户组
一.查询用户及用户组相关命令 1.whoami 查询当前登录的用户名 2.groups 查询当前登录用户名所在的用户组 3.groups root 查询root用户名所在的用户组 二.怎么批量查看用户 ...
- js实现自由落体
实现自由落体运动需要理解的几个简单属性: clientHeight:浏览器客户端整体高度 offsetHeight:对象(比如div)的高度 offsetTop:对象离客户端最顶端的距离 <!d ...
- Latex 出现编辑公式,出现错误 !pdfTex error: Font rntxmi7 at 438 not found
http://docs.miktex.org/manual/advanced.html http://www.cl-projects.de/projects/misc/miktex-fonts.pht ...
- Direct2D 第2篇 绘制椭圆
原文:Direct2D 第2篇 绘制椭圆 #include <windows.h> #include <d2d1.h> #include <d2d1helper.h> ...