Mysql 选择与具有特定值的其他行关联的行
我有关于电话的数据,这些数据以事件的形式显示,如下所示:Mysql 选择与具有特定值的其他行关联的行,mysql,sql,Mysql,Sql,我有关于电话的数据,这些数据以事件的形式显示,如下所示: |event_id|call_id|event_type |service|... |--------|-------|----------------|-------|--- | 1| 1|Call Started |null | | 2| 1|Recorded Message|null | | 3| 1|Call at IVR |null
|event_id|call_id|event_type |service|...
|--------|-------|----------------|-------|---
| 1| 1|Call Started |null |
| 2| 1|Recorded Message|null |
| 3| 1|Call at IVR |null |
| 4| 1|Agent Ringing |Sales |
| 5| 1|Agent Answers |Sales |
| 6| 2|Call Started |null |
| 7| 2|Recorded Message|null |
| 8| 2|Call at IVR |null |
| 9| 1|Disconnected |null |
| 10| 1|Call Ended |null |
| 11| 3|Call Started |null |
| 12| 3|Recorded Message|null |
| 13| 2|Agent Ringing |Support|
| 14| 3|Agent Ringing |Sales |
| 15| 2|Agent Answers |Support|
| 16| 3|Agent Answers |Sales |
| 17| 3|Call Hold |null |
| 18| 2|Disconnected |null |
| 19| 2|Call Ended |null |
| 20| 3|Call Retrieved |Sales |
| 21| 3|Disconnected |null |
| 22| 3|Call Ended |null |
我只想选择与调用Sales
相关的事件。正如您所看到的,只有某些类型的事件在服务
列中包含数据,这是我需要筛选的列。由于处理方式的不同,调用所包含的事件数也不一致
我一直在过滤应用程序中的调用,方法是加载所有事件,然后在call\u id
上使用GroupBy()
,最后通过选择任何具有service
equallingSales
事件的组来过滤这些组
因为在实际数据中有大量我不想要的调用,所以在数据库中过滤这些调用会更有效。我该怎么做
差不多
SELECT event_id
FROM events
GROUP BY call_id
HAVING (SELECT COUNT(*) FROM ***this_group*** WHERE service='Sales') > 0
预期输出应为
|event_id|
|--------|
| 1|
| 2|
| 3|
| 4|
| 5|
| 9|
| 10|
| 11|
| 12|
| 13|
| 16|
| 17|
| 20|
| 21|
| 22|
- 在子查询中,确定所有唯一的
值,该值至少有一行包含call\u id
service='SALES'
- 只需在外部查询中使用此子查询结果来匹配
,并获取所有call\u id
值event\u id
方法2:在以下位置使用
。。在(..)中
- 在子查询中,确定所有唯一的
值,该值至少有一行包含call\u id
service='SALES'
- 只需在外部查询中使用此子查询结果来匹配
,并获取所有call\u id
值event\u id
方法2:在以下位置使用
。。在(..)中
我会使用
存在
:
SELECT e1.event_id
FROM events e1
WHERE EXISTS (SELECT 1 FROM events e2 WHERE e2.call_id = e1.call_id AND e2.service = 'SALES');
我会使用
存在
:
SELECT e1.event_id
FROM events e1
WHERE EXISTS (SELECT 1 FROM events e2 WHERE e2.call_id = e1.call_id AND e2.service = 'SALES');
好的,我想你要找的是如下的东西。我使用了
WITH
查询来挑出您正在寻找的调用,并使用第二个查询从这些调用中挑出事件
WITH salesCalls AS (
SELECT call_id, event_type, service
FROM events
WHERE event_type = "Agent Answers"
AND service = "Sales"
GROUP BY call_id, event_type, service
);
SELECT event_id
FROM events
WHERE call_id IN (SELECT call_id FROM salesCalls)
好的,我想你要找的是如下的东西。我使用了
WITH
查询来挑出您正在寻找的调用,并使用第二个查询从这些调用中挑出事件
WITH salesCalls AS (
SELECT call_id, event_type, service
FROM events
WHERE event_type = "Agent Answers"
AND service = "Sales"
GROUP BY call_id, event_type, service
);
SELECT event_id
FROM events
WHERE call_id IN (SELECT call_id FROM salesCalls)
根本不清楚您是如何获得输出的,或者您正在尝试做什么。你的输出正确吗?如果我理解您正在尝试做什么,它是否应该返回事件14而不是13??根本不清楚您是如何获得输出的,或者您正在尝试做什么。你的输出正确吗?如果我明白你想做什么,它是否应该返回事件14而不是13??