我的问题是我什至不知道这种查询是否可行。我将尝试解释:
我有两张关于电话的表格,“Calls”和“Failed_Calls”。
两个表中的重要列是“Destination”和“Route”,但还有更多列,例如 Id_call 和 start_date 和 called_number 等等。我将省略有关时间段的过滤器以简化。“呼叫”中的目的地可能不存在于“失败呼叫”中,反之亦然。
对于每个可能的 Destination 和 Route 对,我想获得调用计数和失败调用计数,如下面的代码:
select c.Destination, c.Route, count(c.id_call) as Correct, null as Failed
from Calls c
where c.Destination like ('Algeria%')
group by c.Destination, c.Route
union all
select f.Destination, f.Route, null, count(f.id_failed_call) as Failed
from Failed_Calls f
where f.Destination like ('Algeria%')
group by f.Destination, f.Route
表明:
Destination Route Correct Failed
Algeria 9 1 NULL
Algeria Mobile 9 4 NULL
Algeria Mobile 9 NULL 2
...这是正确的,但我需要在单行上显示最后两行的数据,即该目的地和路线的正确和失败呼叫的计数。
我尝试过加入、左加入和不加入,但我总是得到一个错误的计数,比如每对夫妇的通话和通话失败的结果。到目前为止,我最好的镜头是:
select c.Destination, c.Route, count(distinct(c.id_call)) as Correct,
count(distinct(f.id_failed_call)) as Failed
from Calls c, Failed_Calls f
where c.Destination like 'Algeria%'
and f.Destination like 'Algeria%'
group by c.Destination, c.Route
...返回以下内容:
Destination Route Correct Failed
Algeria 9 1 2
Algeria Mobile 9 4 2
“正确”列没问题,但“失败”列显示每行返回的所有目的地的失败调用总和(我已经在查询中检查了更多目的地)。
如果这种查询是可能的,我希望有人可以帮助我。