2013-07-09 219 views
1

我的問題是,我甚至不知道這種查詢是否可能。我會嘗試解釋:mySQL複雜多項查詢

我有兩個關於電話呼叫,「呼叫」和「失敗呼叫」的表格。

這兩個表中的重要列是「目的地」和「路線」,但還有更多像Id_call和start_date和called_number等。我將省略關於時間段的過濾器以簡化。 「呼叫」中的目的地可能不在「失敗呼叫」中,反之亦然。

我想,每一個可能的目的地和路線夫婦,呼叫次數和失敗呼叫數,如下面的代碼:

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 

「正確」的列是確定的,但在「失敗」欄顯示所有失敗呼叫的總和目的地返回,在每一行(我在查詢中檢查了更多目的地)。

如果這種查詢是可能的,我希望有人能幫助我。

+0

請參閱CASE,如在SELECT CASE WHEN ... THEN ... END – Strawberry

回答

0

我在這種情況下沒有嘗試過,但我之前使用過這樣的查詢來總結來自UNION的數據。

select Destination, route, sum(Correct), sum(Failed) from (
    select c.Destination, c.Route, count(c.id_call) as Correct, 0 as Failed 
    from Calls c 
    where c.Destination like ('Algeria%') 
    group by c.Destination, c.Route 
    union all 
    select f.Destination, f.Route, 0 as Correct, count(f.id_failed_call) as Failed 
    from Failed_Calls f 
    where f.Destination like ('Algeria%') 
    group by f.Destination, f.Route) as temp_table 
group by Destination, route; 
+0

此代碼似乎有效。明天我會徹底測試它。謝謝!! – user2564388

+0

它確實有用!非常感謝你! – user2564388

0

勇於嘗試的東西,如:

select c.Destination, c.Route, SUM(c.id_call) as Correct, 
SUM(case when f.id_failed_call is NULL then 0 else 1 end)) as Failed 
from Calls c, Failed_Calls f 
where c.Destination like 'Algeria%' 
and f.Destination like 'Algeria%' 
group by c.Destination, c.Route 
+0

f.id_failed_call永遠不是NULL,它是表鍵。 – user2564388

0

你應該能夠基本包住你的工作查詢到外選擇和組有行;

SELECT Destination, Route, 
     COALESCE(SUM(Correct), 0) Correct, COALESCE(SUM(Failed), 0) Failed 
FROM (
    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 
) 
GROUP BY Destination, Route; 
0

嘗試

SELECT dr.DESTINATION, 
     dr.ROUTE, 
     IFNULL(c.CORRECT_COUNT, 0) AS CORRECT_COUNT, 
     IFNULL(f.FAILED_COUNT, 0) AS FAILED_COUNT 
    FROM (SELECT DISTINCT DESTINATION, ROUTE 
      FROM CALLS 
     UNION DISTINCT 
     SELECT DISTINCT DESTINATION, ROUTE 
      FROM FAILED_CALLS) dr 
    LEFT OUTER JOIN (SELECT DESTINATION, 
          ROUTE, 
          COUNT(DISTINCT ID_CALL) AS CORRECT_COUNT 
        FROM CALLS 
        GROUP BY DESTINATION, ROUTE) c 
    ON (c.DESTINATION = dr.DESTINATION AND 
     c.ROUTE = dr.ROUTE) 
    LEFT OUTER JOIN (SELECT DESTINATION, 
          ROUTE, 
          COUNT(DISTINCT ID_CALL) AS FAILED_COUNT 
        FROM FAILED_CALLS 
        GROUP BY DESTINATION, ROUTE) f 
    ON (f.DESTINATION = dr.DESTINATION AND 
     f.ROUTE = dr.ROUTE) 

dr子查詢獲取目的地和路線的所有可能的組合 - 使用UNION DISTINCT的指示覆印件的,應當刪除。

然後c子查詢加入每個目標和路由的「正確」計數,如果存在這樣一個計數的話。

然後f子查詢加入每個目標和路由的「失敗」計數,如果存在這樣一個計數的話。

Sqlfiddle here

分享和享受。