如果使用带有LEFT JOIN的group by不存在Row,则计为零

时间:2017-03-27 06:37:48

标签: mysql join

我正在使用mysql练习并陷入以下一种情况:

我有两张表格如下。

 Table 1 :
    Distinct_dates having DATE as my_date column name:

    Table 2:my_records having following fields

    record_id,
    record_submitter_name,
    record_submit_date as  DATETIME

    I need to get the records submitted by each submitter for each unique month and it should return 0 if no record found for a month. 

    For example: the below query gives me distinct months from distinct_dates table.
    select distinct(DATE_FORMAT(my_date,'%m-%Y')) from distinct_dates;
    +--------------------------------+
    | (DATE_FORMAT(my_date,'%m-%Y')) |
    +--------------------------------+
    | 01-2016                        |
    | 01-2017                        |
    | 02-2017                        |
    | 03-2017                        |
    +--------------------------------+
    4 rows in set (0.00 sec)

I am able to get the count of Records in each month by submitter and not getting Zero if No Record found for the particular month? 

select distinct(DATE_FORMAT(record_submit_date,'%m-%Y')),count(record_id),record_submitter_name from my_records group by DATE_FORMAT(record_submit_date,'%m-%y'),records_submitter_name;

I have basic understanding of JOINS but not able to get the correct values ,Any help or pointer will be appreciable. 

我需要为每个不同的月份获取每个record_submitter_name的记录计数,如果用户在特定月份没有提交记录,它应该给出零点吗?

进一步使用LEFT Join我能够获得本月不存在数据的不同日期的NULL值,下面是我查询的内容。

select distinct(DATE_FORMAT(d.my_date,'%m-%Y')),count(m.record_id), m.record_submitter_name from distinct_dates d left join my_records m on DATE_FORMAT(m.record_submit_date,'%m-%Y') = DATE_FORMAT(d.my_date,'%m-%Y') and record_submitter_name='xyz' group by d.my_date;

+----------------------------------+----------------+-------------------+
| (DATE_FORMAT(d.my_date,'%m-%Y')) | count(m.record_id) | record_submitter_name |
+----------------------------------+----------------+-------------------+
| 01-2016                          |              1 | xyz               |
| 01-2017                          |              1 | xyz               |
| 02-2017                          |              0 | NULL              |
| 03-2017                          |              0 | NULL              |
+----------------------------------+----------------+-------------------+

但是当使用group by子句时它不起作用: 使用Group By close,我没有收到所需的结果:

select distinct(DATE_FORMAT(d.my_date,'%m-%Y')),count(m.record_id), m.record_submitter_name from distinct_dates d left join my_records m on DATE_FORMAT(m.record_submit_date,'%m-%Y') = DATE_FORMAT(d.my_date,'%m-%Y') group by d.my_date,record_submitter_name;

Following NULL results was not in the output ....
    .
      +----------------------------------+----------------+-------------------+
        | (DATE_FORMAT(d.my_date,'%m-%Y')) | count(m.record_id) | record_submitter_name |
        +----------------------------------+----------------+-------------------+
        | 01-2016                          |              1 | xyz               |
        | 01-2017                          |              1 | xyz               |
        | 02-2017                          |              0 | NULL              |
        | 03-2017                          |              0 | NULL              |
        +----------------------------------+----------------+-------------------+

使用LEFT Join的group by子句有什么问题吗?

由于

3 个答案:

答案 0 :(得分:2)

您需要两个主表格Date Master&另一个是Employee Master并创建两个表的交叉产品,您将获得Month&所有员工的年度明智。

这里我有两张桌子TABLE_A用于日期主人和& TABLE_B作为员工主人

创建交叉产品: SELECT DISTINCT A.MONTH,A.YEAR,A.MONTH_YEAR,B.EMP_ID,B.EMP_NAME FROM TABLE_A A JOIN TABLE_B B ON 1=1

现在在表上执行左连接,您需要为该员工创建记录计数。

SELECT DISTINCT A.MONTH,A.YEAR,A.MONTH_YEAR,B.EMP_ID,B.EMP_NAME 
,COUNT(C.ASSESMENT)AS COUNTS
FROM TABLE_A A
JOIN TABLE_B B
ON 1=1
LEFT JOIN TABLE_C C
ON B.EMP_ID=C.EMP_ID
AND A.MONTH_YEAR=C.MONTH_YEAR
GROUP BY A.MONTH,A.YEAR,A.MONTH_YEAR,B.EMP_ID,B.EMP_NAME 
--ORDER BY EMP_ID,YEAR,MONTH

结果如下: - Result set

答案 1 :(得分:0)

使用左连接,

您应该创建一个包含所有日期的主表。

假设您的记录是从1900开始的,那么该表应该有1900-01-01到2100-01-01记录。

然后,

select 
    distinct(DATE_FORMAT(m.record_submit_date,'%m-%Y')),count(m.record_id),m.record_submitter_name 
from 
    dates d 
    left outer join 
    my_records m on DATE_FORMAT(m.record_submit_date,'%m-%Y') = d.date 
group by d.date,m.records_submitter_name;

答案 2 :(得分:0)

我按照Sunjit的建议创建了另一个Employee主表,以下查询正在按需运行。

SELECT distinct(DATE_FORMAT(A.my_date,'%m-%Y')),B.EMP_NAME ,COUNT(record_id)AS record_count FROM distinct_dates A JOIN employees B ON 1=1 LEFT JOIN my_records C ON B.emp_name=C.record_submitter_name AND DATE_FORMAT(A.my_date,'%m-%Y')=DATE_FORMAT(C.record_submit_d‌​ate,'%m-%Y') GROUP BY A.my_date,'%m-%Y',B.emp_name;