2016-04-21 36 views
1

我有三個表連接兩個表總和計數問題

表1

c_id c_name   status p_id   created_at 
1  john   1   79  2016-04-13 10:36:44  
2  smith   1   79  2016-04-13 14:25:57 
3  phil   1   76  2016-04-18 18:06:21 
4  leo    1   76  2016-04-18 15:51:41 
5  craig   1   79  2016-04-20 10:44:17 
... 

表2

p_id  p_name       
75   test1    
76   test2    
77   test3    
78   test4    
79   test5    
... 

表3

id  c_id       
1   1    
2   1    
3   3    
4   4    
5   4    

我需要的結果:

period  p_id p_name total_count active_count cance_count no_of_occur   
2016-04-13 79  test5 2   2   0   2 
2016-04-18 76  test2 2   2   0   2 
2016-04-20 79  test5 1   1   0   1 
... 

對於代碼,我寫quert,但它給了我錯誤計數

SELECT 
    DATE(DATE_ADD(created_at, INTERVAL 19800 SECOND)) AS period, table1.p_id, 
    MIN(mdcpev.value) AS product_name, 
    COUNT(table1.c_id) AS total_count, 
    SUM(status = 1) AS active_count, 
    SUM(status = 2) AS cancel_count, 
    COUNT(table3.id) AS no_of_occur, 
    FROM table1 
     INNER JOIN table2 AS mdcpev ON mdcpev.p_id = p_id 
     LEFT JOIN table3 AS mdspo ON mdspo.c_id = tabe1.c_id 
     GROUP BY DATE(DATE_ADD(created_at, INTERVAL 19800 SECOND)),p_id 

編輯

我目前的查詢結果是

period  p_id p_name total_count active_count cance_count no_of_occur   
    2016-04-13 79  test5 3   3   0   3 
    2016-04-18 76  test2 3   3   0   2 
    2016-04-20 79  test5 1   1   0   1 

在這裏我得到了3這是錯誤的,它應該是2和no_of_occur應該是2,目前它顯示3 任何人都可以指導我在這個查詢中有什麼問題嗎?

謝謝

+1

你想算什麼請具體說明? –

+0

並提供您當前查詢的輸出以及 – Shadow

+0

請檢查輸出@Shadow –

回答

1

試試這個查詢,它會工作。

SELECT 
     DATE_FORMAT(table1.created_at,'%Y-%m-%d'), 
     table1.p_id, 
     table2.p_name, 
     COUNT(table1.c_id) AS total_count, 
     COUNT(CASE WHEN status = 1 THEN table1.c_id ELSE NULL end) AS active_count, 
     COUNT(CASE WHEN status = 2 THEN table1.c_id ELSE NULL end) AS cancel_count, 
     COUNT(table3.id) AS no_of_occur 
      FROM table1 
      JOIN table2 
      on table2.p_id=table1.p_id 
      JOIN table3 
      ON table3.c_id=table1.c_id 
      Group by DATE_FORMAT(table1.created_at,'%Y-%m-%d'),table1.p_id 

輸出: 我把dumy數據,因爲它從問題和我的系統試圖..

enter image description here

+0

沒有它不工作,它也給了我相同的結果 –

+0

我修改了查詢,刪除不同的現在檢查,因爲沒有明顯我得到正確的輸出,我檢查了我的系統 –

+0

仍然沒有運氣。 .. –

1

差異的臨屋的原因是與表3的加入,因爲你在C_ID列有1和4的兩倍,這些將重複的記錄。

我想改變計數如下:

COUNT(distinct table1.c_id) AS total_count, 
Count(distinct case when status = 1 then table1.c_id else null end) AS active_count, 
Count(distinct case when status = 2 then table1.c_id else null end) AS cancel_count, 
COUNT(distinct table3.id) AS no_of_occur, 

計數內的不同確保只有不同的值進行計數。但是,您仍然會遇到no_of_occur問題。如果這是基於table3.id,那麼對於第1條記錄中的p_id 79,仍然會得到3,因爲table1中的第1條記錄與table3中的2條記錄相關,而table1中的第2條記錄與table3中的第3條記錄相關。所以,我相信你的期望或者不正確,或者你的no_of_occur計算不應該與table3綁定。但在這種情況下,您甚至不需要查詢中的table3。這只是你能回答的問題。

+0

通過使用不同的active_count得到錯誤的計數,因爲你知道如果我有狀態1 3 id然後它只顯示1,但我需要3使用COUNT(distinct table1.c_id)3 active_count –

+0

我得到了正確的total_count。從我+1,但現在我得到了錯誤的狀態計數(active_count和cancel_count) –

+0

你可以更具體地說什麼錯誤的意思? – Shadow

1

SUM(status = 1) AS active_count,這總是返回真(1)的所有條件。所以你得到錯誤的輸出。

我希望這個查詢能工作。請檢查。

SELECT 
    DATE_FORMAT(table1.created_at,'%Y-%m-%d'), 
    table1.p_id, 
    table2.p_name, 
    COUNT(table1.c_id) AS total_count, 
    SUM(CASE WHEN status= 1 THEN 1 ELSE 0 END) AS active_count, 
    SUM(CASE WHEN status= 2 THEN 1 ELSE 0 END) AS cancel_count, 
    COUNT(table3.id) AS no_of_occur 
     FROM table1 
     JOIN table2 
     on table2.p_id=table1.p_id 
     JOIN table3 
     ON table3.c_id=table1.c_id 
     Group by DATE_FORMAT(table1.created_at,'%Y-%m-%d'),table1.p_id 
+0

仍然沒有得到實際結果 –