我遇到了一个问题,我需要为表格选择所需的输出
id | phone | ---- | is_lead_closed | closed_date
-- | -----------| ---- | ---------------| -------------------
1 | 9999999999 | ---- | null | 0000-00-00 00:00:00
2 | 9999999999 | ---- | 1 | 2017-02-01 10:00:00
3 | 9999999999 | ---- | null | 0000-00-00 00:00:00
4 | 9999999999 | ---- | null | 0000-00-00 00:00:00
5 | 9999999999 | ---- | null | 0000-00-00 00:00:00
6 | 9999999999 | ---- | null | 0000-00-00 00:00:00
7 | 9999999999 | ---- | 1 | 2017-02-10 10:00:00
在此之前,我需要输出在前一个线索越近之后的铅的最后计数 即5,这是最后一次关闭后的计数。
好的,我到了这一步 这是我的数据表
id | lms_lead_id | ameyo_lead_id | call_time | is_lead_closed | lead_close_date
177452 | 1387790 | 682 | "2017-02-06 10:07:31" | NULL | "0000-00-00 00:00:00"
182023 | 1387790 | 682 | "2017-02-06 15:09:00" | NULL | "0000-00-00 00:00:00"
182197 | 1387790 | 682 | "2017-02-06 15:20:12" | 1 | "2017-02-06 19:30:47"
182283 | 1387790 | 682 | "2017-02-06 15:29:21" | NULL | "0000-00-00 00:00:00"
183857 | 1387790 | 682 | "2017-02-06 17:30:20" | NULL | "0000-00-00 00:00:00"
187708 | 1387790 | 682 | "2017-02-07 12:38:38" | NULL | "0000-00-00 00:00:00"
188767 | 1387790 | 682 | "2017-02-07 13:31:50" | NULL | "0000-00-00 00:00:00"
191257 | 1387790 | 682 | "2017-02-07 15:36:42" | NULL | "0000-00-00 00:00:00"
192978 | 1387790 | 682 | "2017-02-07 16:44:33" | NULL | "0000-00-00 00:00:00"
195362 | 1387790 | 682 | "2017-02-07 17:47:02" | NULL | "0000-00-00 00:00:00"
196710 | 1387790 | 682 | "2017-02-07 18:33:23" | 1 | "2017-02-07 20:00:33"
209832 | 1387790 | 396 | "2017-02-09 15:33:49" | NULL | "0000-00-00 00:00:00"
到目前为止我创建的查询是
select
a.lms_lead_id,
LENGTH(SUBSTRING_INDEX(group_concat(close_status
SEPARATOR ''),
'1',
- 2)) as attempts,
DATEDIFF(max(call_time), min(call_time)) AS days
from
(select
llct.lms_lead_id,
if(llct.is_lead_closed = 1, 1, 0) as close_status,
llct.call_time
from
lms_ameyo_call_tracker llct
inner join lms_ameyo_call_tracker llct_1 ON llct.lms_lead_id = llct_1.lms_lead_id
and llct_1.is_lead_closed = 1
and date(llct_1.lead_close_date) between '2017-02-07' and '2017-02-07'
and llct.ameyo_lead_id = llct_1.ameyo_lead_id
where
llct.ameyo_lead_id = 682) as a
group by a.lms_lead_id;
其中lms_ameyo_call_tracker是表名,现在我坚持使用call_time
答案 0 :(得分:0)
我假设如果is_lead_closed列的值为1,那么它就是相应引线的闭包。 在这种情况下,您必须使用is_lead_closed = 1获取第二大的最大ID,并使用is_lead_closed = 1获取最高ID。然后计算这两个ID之间的行数。
以下查询应该完成这项工作,但可以进行优化。
从表格中选择count(*),其中id> (从id不在的表中选择max(id)(从表中选择max(id),其中is_lead_closed = 1)和is_lead_closed = 1)和id< =(从表中选择max(id),其中is_lead_closed = 1)