如何在MySQL中查询链接到具有特定ID的另一行的行?

时间:2018-03-15 04:44:30

标签: mysql sql

我正在尝试查询两个不同的表格(CALL_HISTORYHUB_DIRECTORY),以查找“集线器商店”之间的所有通话记录。和一个'辐射商店'。每个调用都有一个CallID字段,并使用发起调用的商店的id创建一个条目,然后为接收调用的每个商店创建一个单独的条目,这些条目都具有该商店的id收到他们。所以它们都具有相同的CallID,但商店ID(DID)对于每个都不同。

问题在于并非所有呼叫都在集线器与其辐条之间,因此我需要将其过滤以仅查找这些记录。

Sample Call Data

RecordID | CallID  | DID   | CallDirection | StartTime 
--------------------------------------------------------
1563486  | 255429  | 492   | Initiated     | 1520870539
1563487  | 255429  | 849   | Received      | 1520870539
1563484  | 255430  | 1098  | Initiated     | 1520870562
1563485  | 255430  | 1098  | Received      | 1520870562
1563482  | 255431  | 307   | Initiated     | 1520870567
1563483  | 255431  | 1013  | Received      | 1520870567
1563506  | 255432  | 1108  | Initiated     | 1520870580
1563509  | 255432  | 1108  | Received      | 1520870580

在这里,您可以看到一个呼叫示例,突出显示的CallID组位于集线器和其辐条之间,其余部分则不是。集线器和辐条在HUB_DIRECTORY中链接在一起,如此:

HUB_DIRECTORY SAMPLE

HubStore | HubDID | SpokeStore | SpokeDID
-----------------------------------------
4        | 37     | Store0004  | 37
4        | 37     | Store0522  | 470
7        | 1083   | Store0007  | 1083
7        | 1083   | Store1000  | 714
7        | 1083   | Store1055  | 759
12       | 38     | Store0012  | 38
12       | 38     | Store1063  | 758
13       | 45     | Store0013  | 45
13       | 45     | Store0337  | 296
13       | 45     | Store1012  | 724

HubDIDSpokeDID字段与DID中的CALL_HISTORY相同。因此,我希望查询DID表格中存在的已启动的来电HUB_DIRECTORY,以及HubDIDSpokeDIDCallID的来电。 1}}还有一个DID的记录,它与相应的hub / spoke相匹配。

我的最终目标如下:

HUB        | Spoke      | Initiated | Received
-----------------------------------------------
Store.0004 | Store.0522 |   304     |   723

我相信我需要使用UNION来获取带有轮毂或轮辐的行,但我无法理解如何完成此操作。

2 个答案:

答案 0 :(得分:1)

我认为此查询会为您提供所需的结果。它适用于您提供的有限样本数据。

select h1.hubstore, h1.hubdid,
       h1.spokestore, h1.spokedid,
       count(distinct if(c2.recordid is null or c1.did!=h1.hubdid, null, c1.recordid)) as initiated, 
       count(distinct if(c2.did!=h1.hubdid, null, c2.recordid)) as received     
from hub_directory h1 
left join (select * from call_history where calldirection='Initiated') c1 
    on c1.did=h1.hubdid or c1.did=h1.spokedid
left join (select * from call_history where calldirection='Received') c2 
    on c2.callid = c1.callid and c2.did=if(c1.did=h1.hubdid, h1.spokedid, h1.hubdid)
group by h1.hubstore, h1.spokestore

根据您fiddle的新示例数据,此查询提供

hubstore    spokestore  initiated   received
355         Store0355   0           0
355         Store0362   0           0
355         Store0655   0           0
357         Store0233   1           2
357         Store0357   0           0
360         Store0360   0           0
360         Store0868   0           0
360         Store1091   0           0
363         Store0363   0           0
363         Store1462   1           0
363         Store1507   1           0
363         Store2507   0           0

答案 1 :(得分:0)

截至目前我所理解的是,您希望获得的数据包括“通过集线器接收的呼叫数量”和“集线器发起的呼叫数”。

select 
a.hubDID, a.CallDirection, count(a.CallDirection) , 
hb.SpokeDID, ch.CallDirection, count(ch.CallDirection)   
from CALL_HISTORY ch inner join 
(
    select RecordID, CallID, DID, CallDirection, StartTime, h.HubStore , h.hubDID
    from CALL_HISTORY c inner join HUB_DIRECTORY h on (c.DID = h.HubDID)
) 
as a 
on ch.CallID = a.CallID 
and a.RecordID <> ch.RecordID
inner join HUB_DIRECTORY hb on hb.SpokeDID = ch.DID

在此尝试Demo

分组数据查询:

select 
a.hubDID, a.CallDirection, count(a.CallDirection) , 
count(hb.SpokeDID),
ch.CallDirection, count(ch.CallDirection)   
from CALL_HISTORY ch inner join 
(
    select distinct c.RecordID, c.CallID, c.DID, c.CallDirection, c.StartTime, c.DID  as hubDID
    from CALL_HISTORY c inner join HUB_DIRECTORY h on (c.DID = h.HubDID)
) 
as a 
on ch.CallID = a.CallID 
and a.RecordID <> ch.RecordID
inner join HUB_DIRECTORY hb on hb.SpokeDID = ch.DID
group by a.hubDID, a.CallDirection,

ch.CallDirection
;

没有组数据查询:

select 
a.hubDID, a.CallDirection, count(a.CallDirection) , 
hb.SpokeDID,hb.SpokeDID,
ch.CallDirection, count(ch.CallDirection)   
from CALL_HISTORY ch inner join 
(
    select distinct c.RecordID, c.CallID, c.DID, c.CallDirection, c.StartTime, c.DID  as hubDID
    from CALL_HISTORY c inner join HUB_DIRECTORY h on (c.DID = h.HubDID)
) 
as a 
on ch.CallID = a.CallID 
and a.RecordID <> ch.RecordID
inner join HUB_DIRECTORY hb on hb.SpokeDID = ch.DID
group by a.hubDID, a.CallDirection,
hb.SpokeDID, 
ch.CallDirection
;