在列中进行子查询

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. 5.jQuery&Ajax

    1.jQuery 什么是 jQuery ? jQuery是一个JavaScript函数库.jQuery是一个轻量级的"写的少,做的多"的JavaScript库.包含以下功能: HT ...

  2. C++ Knowledge series 3

    Programming language evolves always along with Compiler's evolvement The Semantics of Data The size ...

  3. lunix重启service network restart错误Job for network.service failed. See 'system 或Failed to start LSB: Bring

    1.mac地址不对 通过ip addr查看mac地址,然后修改cd /etc/sysconfig/network-scripts/目录下的文件里面的mac地址 2.通过以下方法 systemctl s ...

  4. 使用QJM实现HDFS的HA配置

    使用QJM实现HDFS的HA配置 1.背景 hadoop 2.0.0之前,namenode存在单点故障问题(SPOF,single point of failure),如果主机或进程不可用时,整个集群 ...

  5. 我的visual studio 配色方案 Rubik c++版

    只是更改了c++的配色,放出来与大家分享,因为大胆地采用了各种颜色,所以我把它取名叫做Rubik,因为Rubik‘s cube也就是魔方,我本人是非常喜欢魔方的,然后也符合颜色丰富多彩的这个特征,希望 ...

  6. Uva 11582 巨大的斐波那契数 模运算

    题目链接:https://vjudge.net/contest/156903#problem/A 题意:计算 f(a^b)%n 分析: 1.斐波那契数列是 f(i+2) = f(i+1) + f(i) ...

  7. redis 对cmd的操作

    这个是原子递增的知识点: 关于list部分: 利用lpush命令, rpush命令, lrange命令,对列表操作 此前 我已经 在列表(list)中 插入了 部分 元素了 关于集合set 部分 首先 ...

  8. undefined reference to 'dlopen';undefined reference to 'dlclose';undefined reference to 'dlerror'等问题

    在linux下,编译链接的时候,经常会遇到这样一个问题,undefined reference to.....,引起这个问题的原因在于在链接的时候缺少选项.下面举几个例子,并给出解决办法. 1.  u ...

  9. putty乱码问题

    1.将linux系统编码设置为utf-8 #vi /etc/sysconfig/i18n #设置为如下内容: LANG="en_US.UTF-8" SYSFONT="la ...

  10. JVM性能调优(out of memory内存溢出/泄露出来)

    JVM基础知识: JVM调优工具: 1.jmap jmap常用参数 命令:jmap -heap PID >> D:\heap.log 解释: using thread-local obje ...