my problem is that I don't even know if this kind of query is possible. I'll try to explain:
I have two tables regarding telephone calls, "Calls" and "Failed_Calls".
Important columns in both tables are "Destination" and "Route", but there are many more like Id_call and start_date and called_number and so on. I will omit filters regarding time periods to simplify. It is possible to have destinations in "Calls" not present in "Failed_Calls" and vice versa.
I want to get, for every possible Destination and Route couple, the calls count and the failed calls count, like the following code:
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
It shows:
Destination Route Correct Failed
Algeria 9 1 NULL
Algeria Mobile 9 4 NULL
Algeria Mobile 9 NULL 2
...which is correct, but I need to show the data on the two last rows on a single row, i.e., the count of both correct and failed calls for that destination and route.
I have tried with join, left join and with no join but I always get a bad count, something like the product of calls and failed calls for each couple. My best shot until now is:
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
...which returns the following:
Destination Route Correct Failed
Algeria 9 1 2
Algeria Mobile 9 4 2
The "Correct" column is OK but the "Failed" column shows the sum of failed calls for all destinations returned, in every row (I have checked this with more destinations in the query).
If this kind of a query is possible, I wish someone could help me with it.