码迷,mamicode.com
首页 > 数据库 > 详细

sql 求max和min,但是第二大,第二小怎么算?

时间:2017-07-11 19:24:12      阅读:417      评论:0      收藏:0      [点我收藏+]

标签:bmc   cas   connect   时间   利用   mem   when   phone   联系   

利用子查询,但这样速度较慢。

 

 

SELECT 
 Baoming.id,
  Baoming.addtime AS 报名时间,
  CONCAT(Members.realname,Members.username) AS 客服姓名,
  Sem.name  AS `渠道来源`,
  CASE WHEN bmcs_fd_time IS NOT NULL THEN "分单" ELSE "未分单" END AS `是否分单`,
  CASE WHEN (SELECT COUNT(id) FROM come_go_phone_logs WHERE baoming_id = Baoming.id AND connect_status = 1  AND  call_mode = 2 ) > 0 THEN "接通" ELSE "未接通" END AS `是否接通`,
  (SELECT COUNT(id) FROM come_go_phone_logs WHERE baoming_id = Baoming.id   AND  call_mode = 2 ) AS 联系次数,
  (SELECT MIN(call_time) FROM come_go_phone_logs WHERE baoming_id = Baoming.id  AND  call_mode = 2 ) AS 第一次联系时间,
  (SELECT MIN(call_time) FROM come_go_phone_logs WHERE baoming_id = Baoming.id  AND  call_mode = 2  AND call_time>`第一次联系时间`) AS 第2次联系时间,
  (SELECT MIN(call_time) FROM come_go_phone_logs WHERE baoming_id = Baoming.id   AND  call_mode = 2  AND call_time>`第2次联系时间`) AS 第3次联系时间,
  (SELECT MIN(call_time) FROM come_go_phone_logs WHERE baoming_id = Baoming.id   AND  call_mode = 2  AND call_time>`第3次联系时间`) AS 第4次联系时间
FROM
  `baomings` AS Baoming 
 LEFT JOIN come_go_phone_logs AS Phone
 ON Phone.baoming_id = Baoming.id
 LEFT JOIN `members` AS Members
 ON Phone.seats_cs= Members.`id`
 LEFT JOIN `sem_sources` AS Sem
 ON Sem.j_name=Baoming.bm_laiyuan 
 WHERE Baoming.addtime >= 2017-07-01 00:00:00 
  AND Baoming.is_fp = 1 
  AND Baoming.zx_tuijian = 1 
  AND Baoming.addtime <= 2017-07-11 23:59:59
  AND Baoming.id IN ( SELECT  baoming_id ,COUNT(id) AS c FROM come_go_phone_logs WHERE  connect_status = 1  AND  call_mode = 2  ) 
  GROUP BY Baoming.id

 

sql 求max和min,但是第二大,第二小怎么算?

标签:bmc   cas   connect   时间   利用   mem   when   phone   联系   

原文地址:http://www.cnblogs.com/polax/p/7151970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!