我有以下查询,我想子查询 id 与外部查询匹配,并选择与父查询返回的同一行的值。在这里,它在子查询中返回更多结果,我使用限制 1,但是这样我得到了错误的结果。
select
calldate,
linkedid,
src,
case
when group_concat(disposition, ",") like '%ANSWERED%' then (
select dst
from cdr cd
where
disposition = 'Answered'
and cd.linkedid = cdr.linkedid
and cdr.dst = cd.dst
limit 1
)
else dst
end as status,
did,cnum,
case
when group_concat(disposition, ",") like '%ANSWERED%' then 'ANS'
else 'NO Ans'
end as status
from cdr
where
dst in (
select extension
from users
where name like '%ASMARA%' or name like '%aramsa%'
)
and calldate between '2019-10-31' and '2019-11-01'
group by linkedid
以下是样本数据
| linkedid | uniqueid | calldate | src | dst | recordingfile | billsec | duration |
+-------------------+-------------------+---------------------+----------+------+--------------------------------------------------------------+---------+----------+
| 1572488580.364282 | 1572488595.364292 | 2019-10-31 10:23:15 | 96183245 | 8207 | external-8207-96183245-20191031-102315-1572488595.364292.wav | 0 | 3 |
| 1572489606.364367 | 1572489616.364377 | 2019-10-31 10:40:16 | 96872748 | 8207 | external-8207-96872748-20191031-104016-1572489616.364377.wav | 0 | 2 |
| 1572489995.364421 | 1572490004.364431 | 2019-10-31 10:46:44 | 82672286 | 8207 | external-8207-82672286-20191031-104644-1572490004.364431.wav | 0 | 9 |
| 1572490128.364453 | 1572490141.364465 | 2019-10-31 10:49:01 | 63499024 | 8207 | external-8207-63499024-20191031-104901-1572490141.364465.wav | 0 | 22 |
| 1572491448.364545 | 1572491457.364555 | 2019-10-31 11:10:57 | 96607528 | 8207 | external-8207-96607528-20191031-111057-1572491457.364555.wav | 0 | 11 |
| 1572493373.364735 | 1572493381.364745 | 2019-10-31 11:43:01 | 64969392 | 8207 | external-8207-64969392-20191031-114301-1572493381.364745.wav | 0 | 4 |
| 1572493590.364783 | 1572493600.364797 | 2019-10-31 11:46:40 | 97528558 | 8207 | external-8207-97528558-20191031-114640-1572493600.364797.wav | 0 | 6 |
| 1572495581.364966 | 1572495593.364976 | 2019-10-31 12:19:53 | 97461302 | 8207 | external-8207-97461302-20191031-121953-1572495593.364976.wav | 0 | 6 |
| 1572496788.365064 | 1572496800.365074 | 2019-10-31 12:40:00 | 98273311 | 8207 | external-8207-98273311-20191031-124000-1572496800.365074.wav | 57 | 60 |
| 1572498287.365153 | 1572498303.365164 | 2019-10-31 13:05:03 | 97676522 | 8207 | external-8207-97676522-20191031-130503-1572498303.365164.wav | 151 | 160 |
| 1572501139.365253 | 1572501148.365263 | 2019-10-31 13:52:28 | 96864516 | 8207 | external-8207-96864516-20191031-135228-1572501148.365263.wav | 0 | 28 |
| 1572502316.365345 | 1572502333.365355 | 2019-10-31 14:12:13 | 86722918 | 8207 | external-8207-86722918-20191031-141213-1572502333.365355.wav | 0 | 5 |
| 1572503327.365437 | 1572503343.365452 | 2019-10-31 14:29:03 | 96981919 | 8207 | external-8207-96981919-20191031-142903-1572503343.365452.wav | 0 | 2 |
| 1572504048.365515 | 1572504059.365525 | 2019-10-31 14:40:59 | 86722918 | 8207 | external-8207-86722918-20191031-144059-1572504059.365525.wav | 59 | 61 |
| 1572504878.365590 | 1572504890.365600 | 2019-10-31 14:54:50 | 96510073 | 8207 | external-8207-96510073-20191031-145450-1572504890.365600.wav | 0 | 15 |
| 1572504927.365611 | 1572504940.365623 | 2019-10-31 14:55:40 | 97200511 | 8207 | external-8207-97200511-20191031-145540-1572504940.365623.wav | 0 | 3 |
| 1572504927.365611 | 1572504940.365615 | 2019-10-31 14:56:05 | 97200511 | 8207 | external-8207-97200511-20191031-145540-1572504940.365615.wav | 196 | 196 |
| 1572508104.365882 | 1572508113.365892 | 2019-10-31 15:48:33 | 98763025 | 8207 | external-8207-98763025-20191031-154833-1572508113.365892.wav | 62 | 65 |
| 1572514680.366357 | 1572514699.366371 | 2019-10-31 17:38:19 | 81849860 | 8207 | external-8207-81849860-20191031-173819-1572514699.366371.wav | 0 | 2 |
| 1572518195.366524 | 1572518208.366534 | 2019-10-31 18:36:48 | 97633278 | 8207 | external-8207-97633278-20191031-183648-1572518208.366534.wav | 267 | 269 |
| 1572519995.366615 | 1572520009.366625 | 2019-10-31 19:06:49 | 94555481 | 8207 | external-8207-94555481-20191031-190649-1572520009.366625.wav | 0 | 2 |
| 1572521370.366670 | 1572521387.366680 | 2019-10-31 19:29:47 | 87526998 | 8207 | external-8207-87526998-20191031-192947-1572521387.366680.wav | 0 | 40 |
| 1572521370.366670 | 1572521432.366700 | 2019-10-31 19:30:32 | 87526998 | 8207 | external-8207-87526998-20191031-193032-1572521432.366700.wav | 0 | 19 |
| 1572528655.366787 | 1572528669.366797 | 2019-10-31 21:31:09 | 96778478 | 8207 | external-8207-96778478-20191031-213109-1572528669.366797.wav | 0 | 2 |
| 1572529231.366818 | 1572529245.366828 | 2019-10-31 21:40:45 | 96778478 | 8207 | external-8207-96778478-20191031-214045-1572529245.366828.wav | 0 | 4 |
| 1572488580.364282 | 1572488595.364288 | 2019-10-31 10:23:15 | 96183245 | 8208 | external-8208-96183245-20191031-102315-1572488595.364288.wav | 0 | 3 |
| 1572489606.364367 | 1572489616.364373 | 2019-10-31 10:40:16 | 96872748 | 8208 | external-8208-96872748-20191031-104016-1572489616.364373.wav | 0 | 2 |
| 1572489995.364421 | 1572490004.364427 | 2019-10-31 10:46:44 | 82672286 | 8208 | external-8208-82672286-20191031-104644-1572490004.364427.wav | 0 | 9 |
| 1572490128.364453 | 1572490141.364461 | 2019-10-31 10:49:01 | 63499024 | 8208 | external-8208-63499024-20191031-104901-1572490141.364461.wav | 0 | 22 |
| 1572491448.364545 | 1572491457.364551 | 2019-10-31 11:10:57 | 96607528 | 8208 | external-8208-96607528-20191031-111057-1572491457.364551.wav | 0 | 11 |
| 1572493373.364735 | 1572493381.364741 | 2019-10-31 11:43:01 | 64969392 | 8208 | external-8208-64969392-20191031-114301-1572493381.364741.wav | 0 | 4 |
| 1572493590.364783 | 1572493600.364793 | 2019-10-31 11:46:40 | 97528558 | 8208 | external-8208-97528558-20191031-114640-1572493600.364793.wav | 0 | 6 |
| 1572495581.364966 | 1572495593.364972 | 2019-10-31 12:19:53 | 97461302 | 8208 | external-8208-97461302-20191031-121953-1572495593.364972.wav | 0 | 6 |
| 1572496788.365064 | 1572496800.365070 | 2019-10-31 12:40:00 | 98273311 | 8208 | external-8208-98273311-20191031-124000-1572496800.365070.wav | 0 | 2 |
| 1572498287.365153 | 1572498303.365160 | 2019-10-31 13:05:03 | 97676522 | 8208 | external-8208-97676522-20191031-130503-1572498303.365160.wav | 0 | 8 |
| 1572501139.365253 | 1572501148.365259 | 2019-10-31 13:52:28 | 96864516 | 8208 | external-8208-96864516-20191031-135228-1572501148.365259.wav | 0 | 28 |
| 1572502316.365345 | 1572502333.365351 | 2019-10-31 14:12:13 | 86722918 | 8208 | external-8208-86722918-20191031-141213-1572502333.365351.wav | 0 | 5 |
| 1572503327.365437 | 1572503343.365448 | 2019-10-31 14:29:03 | 96981919 | 8208 | external-8208-96981919-20191031-142903-1572503343.365448.wav | 212 | 215 |
| 1572504048.365515 | 1572504059.365521 | 2019-10-31 14:40:59 | 86722918 | 8208 | external-8208-86722918-20191031-144059-1572504059.365521.wav | 0 | 2 |
| 1572504927.365611 | 1572504940.365619 | 2019-10-31 14:55:40 | 97200511 | 8208 | external-8208-97200511-20191031-145540-1572504940.365619.wav | 0 | 3 |
| 1572504878.365590 | 1572504890.365596 | 2019-10-31 14:54:50 | 96510073 | 8208 | external-8208-96510073-20191031-145450-1572504890.365596.wav | 69 | 85 |
| 1572508104.365882 | 1572508113.365888 | 2019-10-31 15:48:33 | 98763025 | 8208 | external-8208-98763025-20191031-154833-1572508113.365888.wav | 0 | 3 |
| 1572514680.366357 | 1572514699.366367 | 2019-10-31 17:38:19 | 81849860 | 8208 | external-8208-81849860-20191031-173819-1572514699.366367.wav | 58 | 61 |
| 1572518195.366524 | 1572518208.366530 | 2019-10-31 18:36:48 | 97633278 | 8208 | external-8208-97633278-20191031-183648-1572518208.366530.wav | 0 | 2 |
| 1572519995.366615 | 1572520009.366621 | 2019-10-31 19:06:49 | 94555481 | 8208 | external-8208-94555481-20191031-190649-1572520009.366621.wav | 199 | 201 |
| 1572521370.366670 | 1572521387.366676 | 2019-10-31 19:29:47 | 87526998 | 8208 | external-8208-87526998-20191031-192947-1572521387.366676.wav | 0 | 40 |
| 1572521370.366670 | 1572521432.366696 | 2019-10-31 19:30:32 | 87526998 | 8208 | external-8208-87526998-20191031-193032-1572521432.366696.wav | 0 | 19 |
| 1572528655.366787 | 1572528669.366793 | 2019-10-31 21:31:09 | 96778478 | 8208 | external-8208-96778478-20191031-213109-1572528669.366793.wav | 140 | 142 |
| 1572529231.366818 | 1572529245.366824 | 2019-10-31 21:40:45 | 96778478 | 8208 | external-8208-96778478-20191031-214045-1572529245.366824.wav | 75 | 79 |
| 1572488580.364282 | 1572488595.364284 | 2019-10-31 10:23:15 | 96183245 | 8209 | external-8209-96183245-20191031-102315-1572488595.364284.wav | 0 | 3 |
| 1572489606.364367 | 1572489616.364369 | 2019-10-31 10:40:16 | 96872748 | 8209 | external-8209-96872748-20191031-104016-1572489616.364369.wav | 0 | 2 |
| 1572489995.364421 | 1572490004.364423 | 2019-10-31 10:46:44 | 82672286 | 8209 | external-8209-82672286-20191031-104644-1572490004.364423.wav | 0 | 9 |
| 1572490128.364453 | 1572490141.364457 | 2019-10-31 10:49:01 | 63499024 | 8209 | external-8209-63499024-20191031-104901-1572490141.364457.wav | 0 | 22 |
| 1572491448.364545 | 1572491457.364547 | 2019-10-31 11:10:57 | 96607528 | 8209 | external-8209-96607528-20191031-111057-1572491457.364547.wav | 0 | 11 |
| 1572493373.364735 | 1572493381.364737 | 2019-10-31 11:43:01 | 64969392 | 8209 | external-8209-64969392-20191031-114301-1572493381.364737.wav | 0 | 5 |
| 1572493590.364783 | 1572493600.364789 | 2019-10-31 11:46:40 | 97528558 | 8209 | external-8209-97528558-20191031-114640-1572493600.364789.wav | 0 | 6 |
| 1572495581.364966 | 1572495593.364968 | 2019-10-31 12:19:53 | 97461302 | 8209 | external-8209-97461302-20191031-121953-1572495593.364968.wav | 0 | 6 |
| 1572496788.365064 | 1572496800.365066 | 2019-10-31 12:40:00 | 98273311 | 8209 | external-8209-98273311-20191031-124000-1572496800.365066.wav | 0 | 2 |
| 1572498287.365153 | 1572498303.365156 | 2019-10-31 13:05:03 | 97676522 | 8209 | external-8209-97676522-20191031-130503-1572498303.365156.wav | 0 | 8 |
| 1572501139.365253 | 1572501148.365255 | 2019-10-31 13:52:28 | 96864516 | 8209 | external-8209-96864516-20191031-135228-1572501148.365255.wav | 0 | 28 |
以下是样本数据。linkedid 相同表示相同的呼叫,因此需要显示在一条记录中,如果在任何未回答的行中回答,则处置已回答。
| calldate | linkedid | src | status | did | cnum | status |
+---------------------+-------------------+----------+--------+-----+----------+--------+
| 2019-10-31 10:23:15 | 1572488580.364282 | 96183245 | 8210 | | 96183245 | ANS |
| 2019-10-31 10:40:16 | 1572489606.364367 | 96872748 | 8210 | | 96872748 | ANS |
| 2019-10-31 10:46:44 | 1572489995.364421 | 82672286 | 8210 | | 82672286 | ANS |
| 2019-10-31 10:49:01 | 1572490128.364453 | 63499024 | 8210 | | 63499024 | ANS |
| 2019-10-31 11:10:57 | 1572491448.364545 | 96607528 | 8210 | | 96607528 | ANS |
| 2019-10-31 11:43:01 | 1572493373.364735 | 64969392 | 8210 | | 64969392 | ANS |
| 2019-10-31 11:46:40 | 1572493590.364783 | 97528558 | 8210 | | 97528558 | ANS |
| 2019-10-31 12:19:53 | 1572495581.364966 | 97461302 | 8210 | | 97461302 | ANS |
| 2019-10-31 12:40:00 | 1572496788.365064 | 98273311 | 8207 | | 98273311 | ANS |
| 2019-10-31 13:05:03 | 1572498287.365153 | 97676522 | 8207 | | 97676522 | ANS |
| 2019-10-31 13:52:28 | 1572501139.365253 | 96864516 | 8210 | | 96864516 | ANS |
| 2019-10-31 14:12:13 | 1572502316.365345 | 86722918 | 8210 | | 86722918 | ANS |
| 2019-10-31 14:29:03 | 1572503327.365437 | 96981919 | 8208 | | 96981919 | ANS |
| 2019-10-31 14:42:01 | 1572504048.365515 | 86722918 | 8207 | | 86722918 | ANS |
| 2019-10-31 14:54:50 | 1572504878.365590 | 96510073 | 8208 | | 96510073 | ANS |
| 2019-10-31 14:55:40 | 1572504927.365611 | 97200511 | 8207 | | 97200511 | ANS |
| 2019-10-31 15:48:33 | 1572508104.365882 | 98763025 | 8207 | | 98763025 | ANS |
| 2019-10-31 17:38:19 | 1572514680.366357 | 81849860 | 8208 | | 81849860 | ANS |
| 2019-10-31 18:36:48 | 1572518195.366524 | 97633278 | 8207 | | 97633278 | ANS |
| 2019-10-31 19:06:49 | 1572519995.366615 | 94555481 | 8208 | | 94555481 | ANS |
| 2019-10-31 19:29:47 | 1572521370.366670 | 87526998 | 8209 | | 87526998 | NO Ans |
| 2019-10-31 21:31:09 | 1572528655.366787 | 96778478 | 8208 | | 96778478 | ANS |
| 2019-10-31 21:40:45 | 1572529231.366818 | 96778478 | 8208 | | 96778478 | ANS |
+---------------------+-------------------+----------+--------+-----+----------+--------+```