加入两个 table 并求和计数问题

Join two table and sum count issue

我有三个table

table 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
...

table 2

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

table 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

这里我得到 active_count 3 这是错误的,它应该是 2 而 no_of_occur 应该是 2 目前它显示 3 任何人都可以指导我了解此查询中的错误吗?

谢谢

造成差异的原因是与表 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 中的第二条记录与第 3 条记录相关记录在表 3 中。因此,我认为您的期望要么不正确,要么您的 no_of_occur 计算根本不应与 table3 相关联。但在这种情况下,您甚至不需要在查询中使用 table3。这是 sg 只有你可以回答。

SUM(status = 1) AS active_count,这对于所有条件总是 return true(1)。所以你得到了错误的输出。

我希望这个查询能够work.Please检查。

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

试试这个查询,它会起作用。

    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

输出: 我从问题中获取了虚拟数据并在我的系统中进行了尝试..