Mysql SQL查询以获取上次语句连接相等的客户端数 我需要做一个SQL查询
表“记录”结构:Mysql SQL查询以获取上次语句连接相等的客户端数 我需要做一个SQL查询,mysql,sql,Mysql,Sql,表“记录”结构: contact_id(integer), client_id(integer), worker_id(integer), statement_status(varchar), contact_ts(timestamp) 它必须显示以下内容: current date number of clients which last statement_status was 'interested' number of clients which last statement_
contact_id(integer),
client_id(integer),
worker_id(integer),
statement_status(varchar),
contact_ts(timestamp)
它必须显示以下内容:
current date
number of clients which last statement_status was 'interested'
number of clients which last statement_status was 'not_interested' and previus status was 'not_present'
有人能帮忙吗
样本数据:
contact_id client_id contact_ts worker_id statement_status
'1', '181', '2017-09-24 03:38:31.000000', '107', 'voicemail'
'2', '72', '2017-09-23 09:32:38.000000', '10', 'not_interested'
'3', '277', '2017-09-22 07:06:16.000000', '119', 'interested'
'4', '36', '2017-09-21 04:39:57.000000', '118', 'not_present'
'5', '33', '2017-09-20 04:12:12.000000', '161', 'voicemail'
'6', '244', '2017-09-19 02:26:30.000000', '13', 'not_interested'
'7', '346', '2017-09-18 02:30:35.000000', '255', 'interested'
'8', '128', '2017-09-17 06:20:13.000000', '52', 'not_present'
'9', '33', '2017-09-16 08:58:02.000000', '188', 'not_present'
'10', '352', '2017-09-15 08:18:40.000000', '324', 'not_interested'
'11', '334', '2017-09-14 04:27:40.000000', '373', 'interested'
'12', '2', '2017-09-13 08:44:40.000000', '40', 'not_present'
'13', '33', '2017-09-12 03:46:16.000000', '252', 'voicemail'
'14', '366', '2017-09-11 04:31:22.000000', '78', 'not_interested'
'15', '184', '2017-09-10 06:08:01.000000', '289', 'interested'
'16', '184', '2017-09-09 05:45:56.000000', '124', 'not_present'
'17', '102', '2017-09-08 07:09:30.000000', '215', 'voicemail'
'18', '140', '2017-09-07 08:09:18.000000', '196', 'not_interested'
'19', '315', '2017-09-06 05:13:40.000000', '242', 'interested'
'20', '268', '2017-09-05 07:41:40.000000', '351', 'not_present'
'21', '89', '2017-09-04 05:32:05.000000', '232', 'voicemail'
期望输出:
我尝试了一些子查询,但显然不起作用:
SELECT
GETDATE()
,(select count(*)
from record a
where (select statement_status
from record
where client_id == a.client_id
order by a.contact_ts
limit 1) == "interested"
group by a.contact_id)
,(select count(*)
from record a
where (select (select statement_status
from record
where client_id == a.client_id
order by a.contact_ts
limit 2) order by a.contact_ts desc limit 1) == "interested"
and
(select statement_status
from record
where client_id == a.client_id
order by a.contact_ts
limit 1) == "interested"
group by a.contact_id)
from record b;
我应该如何使用内部选择?
我必须写一首诗,因为我的大部分帖子都是代码。
也许是死人的东西
“不要让太阳在你屁股上烧个洞,威廉·布莱克。现在起来,开你的车,在死人的骨头上犁地!”
) 试试这样的方法: 身份为 选择不同的客户端id, w1上的第一个状态作为最后一个状态, 第n个值声明状态,2比w1为上一个状态 根据记录 窗口w1作为按客户id划分的分区按联系人顺序描述无界前向和无界后向之间的范围 选择当前日期, SUMlast_状态=“感兴趣”为感兴趣, SUMlast_status='not_interest'和prev_status='not_present'为not_interest 来自身份
请显示示例数据和预期输出,以及您尝试过的内容,并在CTE中读取每个客户端的枚举行,在查询中查找相应行,然后应用条件聚合。您使用的是哪个版本的mysql?也许您可以使用窗口函数来捕获以前状态的案例。如果你正在准备一个简单的例子,请放一些可以直接运行的sql代码。我想这是我想要的最新版本。唐克斯兄弟。
SELECT
GETDATE()
,(select count(*)
from record a
where (select statement_status
from record
where client_id == a.client_id
order by a.contact_ts
limit 1) == "interested"
group by a.contact_id)
,(select count(*)
from record a
where (select (select statement_status
from record
where client_id == a.client_id
order by a.contact_ts
limit 2) order by a.contact_ts desc limit 1) == "interested"
and
(select statement_status
from record
where client_id == a.client_id
order by a.contact_ts
limit 1) == "interested"
group by a.contact_id)
from record b;