在列中进行子查询

1、在一个表中有多个员工ID,比如一个下单员工,一个修改订单的员工,可以使用在列中进行子查询,具体如下:

   (
SELECT
staff_name
FROM
sp_staff_basic
WHERE
sp_staff_basic.id = qc_return_progress_record.created_by
) returnProcessName, LEFT JOIN sp_staff_basic ON sp_staff_basic.id = qc_returnrervice.updated_by

2、left join 自己的表,比如说在一个表中,有一个出发时间字段,又有一个到达时间字段,两个表所对应的节点不一致,但是两个表都记录在订单表中,比如订单的一个状态为出发吗,一个状态为到达,我们需要计算路途中所花的时长,left join 自己的表,就可以取出两个节点所对应的时间,具体如下:

select
sp_staff_usework.attach_group attachGroup,COUNT(sp_staff_usework.attach_group) countFrequency,sp_staff_basic.id staffId
from (SELECT * from or_order_state_record where or_order_state_record.state_type=3) n1
LEFT JOIN (SELECT * from or_order_state_record where or_order_state_record.state_type=4) n2 on n1.order_id=n2.order_id
LEFT JOIN sp_staff_basic on sp_staff_basic.id=n1.created_by
LEFT JOIN sp_staff_usework on sp_staff_usework.staff_id=n1.created_by where TIMESTAMPDIFF(MINUTE,n1.created_date,n2.created_date)>3
GROUP BY sp_staff_usework.attach_group

这种方法还可以用来做统计表,表中有多个统计字段的,比如我下面做的客服的服务质量表:

        SELECT
sp_staff_basic.id staffId,
sp_staff_usework.attach_group attachGroup,
t1.countFrequency timeoutAudit,
t2.countFrequency timeOutList,
t3.countMis countMis,
t4.countOrderChange countOrderChange,
t5.countPeople countPeople,
sp_staff_basic.staff_name staffName,
sp_staff_usework.position_code positionCode,
sp_staff_usework.position_class postionClass,
sp_company.id companyId,
sp_company.company_name companyName
from sp_staff_basic
LEFT JOIN sp_staff_usework
on sp_staff_usework.staff_id=sp_staff_basic.id
LEFT JOIN(
select
sp_staff_usework.attach_group attachGroup,COUNT(sp_staff_usework.attach_group) countFrequency,sp_staff_basic.id staffId
from (SELECT * from or_order_state_record where or_order_state_record.state_type=3) n1
LEFT JOIN (SELECT * from or_order_state_record where or_order_state_record.state_type=4) n2 on n1.order_id=n2.order_id
LEFT JOIN sp_staff_basic on sp_staff_basic.id=n1.created_by
LEFT JOIN sp_staff_usework on sp_staff_usework.staff_id=n1.created_by
<where>
<if test="param.startCreateDate != null">
AND n1.created_date <![CDATA[ >= ]]> #{param.startCreateDate,jdbcType=TIMESTAMP}
</if>
<if test="param.endCreateDate != null">
AND n1.created_date <![CDATA[ < ]]> date_add(#{param.endCreateDate,jdbcType=TIMESTAMP},interval 1 day)
</if>
and TIMESTAMPDIFF(MINUTE,n1.created_date,n2.created_date)>3
</where>
GROUP BY sp_staff_usework.attach_group
)t1 on sp_staff_usework.attach_group=t1.attachGroup
LEFT JOIN(
select
sp_staff_usework.attach_group attachGroup,COUNT(sp_staff_usework.attach_group) countFrequency,sp_staff_basic.id staffId
from (SELECT * from or_order_state_record where or_order_state_record.state_type=0) n1
LEFT JOIN (SELECT * from or_order_state_record where or_order_state_record.state_type=3) n2 on n1.order_id=n2.order_id
LEFT JOIN sp_staff_basic on sp_staff_basic.id=n1.created_by
LEFT JOIN sp_staff_usework on sp_staff_usework.staff_id=n1.created_by
<where>
<if test="param.startCreateDate != null">
AND n1.created_date <![CDATA[ >= ]]> #{param.startCreateDate,jdbcType=TIMESTAMP}
</if>
<if test="param.endCreateDate != null">
AND n1.created_date <![CDATA[ < ]]> date_add(#{param.endCreateDate,jdbcType=TIMESTAMP},interval 1 day)
</if>
and TIMESTAMPDIFF(MINUTE,n1.created_date,n2.created_date)>3
</where>
GROUP BY sp_staff_usework.attach_group
)t2 on sp_staff_usework.attach_group=t2.attachGroup
LEFT JOIN (
SELECT COUNT(qc_mistakenum.mistakenum_id) countMis,qc_mistakenum.staff_id staffId,qc_mistakenum.created_date from qc_mistakenum
<where>
<if test="param.startCreateDate != null">
AND qc_mistakenum.created_date <![CDATA[ >= ]]> #{param.startCreateDate,jdbcType=TIMESTAMP}
</if>
<if test="param.endCreateDate != null">
AND qc_mistakenum.created_date <![CDATA[ < ]]> date_add(#{param.endCreateDate,jdbcType=TIMESTAMP},interval 1 day)
</if>
</where>
) t3 on t3.staffId=sp_staff_basic.id
LEFT JOIN (
SELECT count(qc_orderchange.order_id) countOrderChange,qc_orderchange.created_by,qc_orderchange.created_date from qc_orderchange
<where>
<if test="param.startCreateDate != null">
AND qc_orderchange.created_date <![CDATA[ >= ]]> #{param.startCreateDate,jdbcType=TIMESTAMP}
</if>
<if test="param.endCreateDate != null">
AND qc_orderchange.created_date <![CDATA[ < ]]> date_add(#{param.endCreateDate,jdbcType=TIMESTAMP},interval 1 day)
</if>
</where>
) t4 on t4.created_by=sp_staff_basic.id
LEFT JOIN (
select qc_complaint_people.comp_people_id,count(qc_complaint_people.comp_people_id) countPeople,qc_complaint_record.created_date
from qc_complaint_people
LEFT JOIN sp_staff_basic
on sp_staff_basic.id=qc_complaint_people.comp_people_id
LEFT JOIN qc_complaint_record
on qc_complaint_record.comp_id=qc_complaint_people.comp_id
<where>
<if test="param.startCreateDate != null">
AND qc_complaint_record.created_date <![CDATA[ >= ]]> #{param.startCreateDate,jdbcType=TIMESTAMP}
</if>
<if test="param.endCreateDate != null">
AND qc_complaint_record.created_date <![CDATA[ < ]]> date_add(#{param.endCreateDate,jdbcType=TIMESTAMP},interval 1 day)
</if>
</where>
GROUP BY qc_complaint_people.comp_people_id
) t5 on t5.comp_people_id=sp_staff_basic.id
left join sp_company
on sp_company.id=sp_staff_usework.company_id
<where>
<if test="param.companyId != null">
and sp_company.id = #{param.companyId,jdbcType=VARCHAR}
</if>
<if test="param.staffId != null and param.staffId != ''">
and sp_staff_basic.id = #{param.staffId,jdbcType=INTEGER}
</if>
<if test="param.positionCode != null and param.positionCode != ''">
and sp_staff_usework.position_code = #{param.positionCode,jdbcType=INTEGER}
</if>
and (sp_staff_usework.position_code='KHFWZX_KFDDDY' or sp_staff_usework.position_code='KHFWZX_ZJKFDDDY' or
sp_staff_usework.position_code='KHFWZX_GJKFDDDY' or sp_staff_usework.position_code='ALL_ZGSKFGJJL' or
sp_staff_usework.position_code='ALL_ZGSKFJL' or sp_staff_usework.position_code='KHFWZX_ZSKFDDDY'
)
</where>
ORDER BY sp_staff_basic.id

最后的原型为,具体是使用员工去查询他的职位,通过职位去筛选其他的,具体见上sql脚本:

还有一个为服务满意度的

服务满意度的做了好久,在 AND 与 OR 的合用与业务逻辑上卡了蛮久的时间

首先

我们需要过滤出需要回访的

需要回访的为:

待回访跟踪中仅包含:电话回访-待回访/未接通;短信回访-超时未提交/不满意-且电话回访为待回访的/非常不满意-且电话回访为待回访的,见列表中的集中组合字段

只有在列表中的字段需要进行回访,其他的直接过滤到完成回访的列表,且还需要过滤点击回访,回访结果为未接通字段

所以在

待回访的sql脚本中我们使用的过滤条件为:

WHERE
or_task_node.node_type = 8
AND (
cs_customer.default_return_type IN (0, 1)
AND qc_returnrervice.sms_return_result IN (2, 7, 9, 3)
AND qc_returnrervice.phone_return_result IN (2, 3)
AND (
qc_return_satisfied_record.return_result IS NULL
OR qc_return_satisfied_record.return_result = 2
)
)
GROUP BY
or_rescue_order.id
ORDER BY
qc_returnrervice.created_date DESC

回访完成后,需要将其他结果与上面过滤条件过滤掉的字段信息全部进行显示,所以用 AND(A OR B)的结构进行筛选

WHERE
or_task_node.node_type = 8
AND (
(
qc_returnrervice.sms_return_result NOT IN (2, 7, 9, 3)
AND qc_returnrervice.phone_return_result NOT IN (2, 3)
)
OR (
qc_returnrervice.sms_return_result IN (2, 7, 9, 3)
AND qc_returnrervice.phone_return_result IN (2, 3)
AND qc_return_satisfied_record.return_result IN (0, 1)
)
)
GROUP BY
or_rescue_order.id
ORDER BY
qc_returnrervice.created_date DESC

进行相反的过滤,这样就能展示出来所有的回访信息

工作中遇到的比较奇怪的一些sql(一些子查询)的更多相关文章

  1. SQL Fundamentals: 子查询 || WHERE,HAVING,FROM,SELECT子句中使用子查询,WITH子句

    SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...

  2. 关于工作中.net转java遇到的一个远程调用传递重复参的问题。

    工作中遇到一个很奇怪的传参问题.之前.net使用的是一个List列表,列表中有几个重复的参数.列表中使用的model类是KeyValue. 我使用java模仿其写法,传递List和KeyValue.对 ...

  3. 工作中遇到的oracle分页查询问题及多表查询相关

    在工作中,有时,我们会用到oracle分页查询.这时,就需要先了解oracle的rownum.rowmun是oracle的伪列,只能用符号(<.<=.!=),而不能用这些符号(>,& ...

  4. 工作中遇到的令人头疼的bug

    工作中我们会遇到形形色色的bug,但是很多bug都可以调试很明显的看出来,这种bug解决起来我们不会那么头疼但是有些却让人头疼而捉急,特别是本地运行一切正常,上传服务器就会出现bug.现在我总结几个我 ...

  5. SQL基本操作(工作中够用了)

      以下文章内容都是我自己从平时学习SQL语言时整理而来,写这篇文章是希望我或大家在使用能更方便的查询. 如果有不完整或不正确的地方请大家指出~谢谢大家 基本SQL操作 创建数据库 CREATE DA ...

  6. 浅谈T-SQL中的子查询

    引言 这篇文章我们来简单的谈一下子查询的相关知识.子查询可以分为独立子查询和相关子查询.独立子查询不依赖于它所属的外部查询,而相关子查询则依赖于它所属的外部查询.子查询返回的值可以是标量(单值).多值 ...

  7. 在 SQL Server 数据库的 WHERE 语句中使用子查询

    这是关于子查询语句的一系列文章中的第三篇.在这篇文章中我们将讨论WHERE语句中的子查询语句.其他的文章讨论了其他语句中的子查询语句. 本次课程中的所有例子都是基于Microsoft SQL Serv ...

  8. SELECT中(非常)常用的子查询操作

    MySQL中的子查询 是在MySQL中经常使用到的一个操作,不仅仅是用在DQL语句中,在DDL语句.DML语句中也都会常用到子查询. 子查询的定义: 子查询是将一个查询语句嵌套在另一个查询语句中: 在 ...

  9. SELECT中常用的子查询操作

    MySQL中的子查询 是在MySQL中经常使用到的一个操作,不仅仅是用在DQL语句中,在DDL语句.DML语句中也都会常用到子查询. 子查询的定义: 子查询是将一个查询语句嵌套在另一个查询语句中: 在 ...

随机推荐

  1. Sass基础(二)

    五.嵌套 在Sass中,嵌套有三种方式:选择器嵌套.属性嵌套.伪类嵌套 1.选择器嵌套 2.属性嵌套 3.伪类嵌套 六.混合宏 当样式变得越来越复杂,需要重复使用大段的样式时,使用变量就无法达到目的了 ...

  2. JavaScript流程控制语句脑图

    JavaScript流程控制语句脑图 图片是从网上找来的,在这记录一下,以备后面需要的时候查找方便. JavaScript通过规定的语句让有条件的按照一定的方式执行. 分为:循环语句 while do ...

  3. spring笔记4-事务管理

    一.xml配置文件形式 通过转账案例,学习事务管理 1.建立数据库 2.编写entity package huguangqin.com.cnblogs.entity; public class Use ...

  4. c++ 处理utf-8字符串

    c++的字符串中的每一个元素都是一个字节.所以在装入utf8字符串的时候,其实是按照一定的规则编码的. 字符的8位中 如果0开头 则自己就是一个单位. 1字节 0xxxxxxx  2字节 110xxx ...

  5. 还是要精简开发呀,VS2015太大,VS2010不想装

    公司电脑配置没有很好,所以对于我就是一个挑战. vs2015装上了,但是一打开就卡卡卡,基本没法办公. 公布能用记事本吧,太多不方便: Notepad++做辅助的局部修改还是很好用的,装上插件就智能提 ...

  6. Thymeleaf 随记

    一.基础写法: th:text='${数据}  ,其中text可以修改成其他,如href,value,class....看需求 <p th:text='${后台返回的数据}'>静态文本&l ...

  7. April 22 2017 Week 16 Saturday

    Fear is an essential part of our survival, it keeps us alert. 恐惧是生存的重要部分,它让我们保持警惕. Fear and pain are ...

  8. IOS item属性总结

    一.UINavigationItem1> 获得方式self.navigationItem // self是指控制器 2> 作用可以用来设置当前控制器顶部导航栏的内容// 设置导航栏中间的内 ...

  9. mongodb在C#的连接以及curd写法

    连接数据库:参考地址:https://blog.oz-code.com/how-to-mongodb-in-c-part-2/ // Empty ctor will get you a // clie ...

  10. android获取传感器数据

    传感器获取数据的频率: https://blog.csdn.net/huangbiao86/article/details/6745933 SensorManager.SENSOR_DELAY_GAM ...