简体   繁体   中英

Return 0 value record if record does not exist with unique id in Mysql

I have two tables, verified_users and cfw_documents. I wanted to show no of documents uploaded by each user in each month.

I tried below query-

SELECT  a.user_id,count(distinct d.db_filename), MONTH(d.doc_date) as upload_month
FROM verified_users a
join cfw_documents d on a.user_id=d.user_id and d.doc_date >= a.created_on
GROUP BY 1,3 ORDER BY 1;

I got the output as :

user_ id   count      month
1          15         3    
1          20         6    
2          5          1    
2          1          4

There are missing months for each user where user did not uploaded the records. I wanted to show such months with count 0.

Expected output:

user_ id   count      month
1          15         3
1           0         4 
1           0         5 
1          20         6
1           0         7
1           0         8
1           0         9    
2           5         1
2           0         2
2           0         3 
2           1         4
2           0         5
2           0         6

I tried to search but all solutions have only count and month columns but not other column from table. Kindly suggest approach for this problem.

You should use a list for month and left join (or use a table for month number)

  SELECT  a.user_id, ifnull(count(distinct d.db_filename), 0), t.month  as upload_month
  FROM (

    select  1  month from dual 
    union
    select  2  from dual 
    union
    select  3  from dual 
    union
    select  4  from dual 
    union
    select  5  from dual 
    union
    select  6  from dual 
    union
    select  7  from dual 
    union
    select  8  from dual 
    union
    select  9  from dual 
    union
    select  10  from dual 
    union
    select  11  from dual 
    union
    select  12  from dual 

  ) t 
  left join user_documents a on t.month = MONTH(d.doc_date)
  join  cfw_documents d on a.user_id=d.user_id and d.doc_date >= a.created_on
  GROUP BY 1,3 ORDER BY 1;

looking to your sample seems you need result only for macthing month so you should use inner join

      SELECT  a.user_id, ifnull(count(distinct d.db_filename), 0), t.month  as upload_month
      FROM (

        select  1  month from dual 
        union
        select  2  from dual 
        union
        select  3  from dual 
        union
        select  4  from dual 
        union
        select  5  from dual 
        union
        select  6  from dual 
        union
        select  7  from dual 
        union
        select  8  from dual 
        union
        select  9  from dual 
        union
        select  10  from dual 
        union
        select  11  from dual 
        union
        select  12  from dual 

      ) t 
      INNER join user_documents a on t.month = MONTH(d.doc_date)
      INNER join  cfw_documents d on a.user_id=d.user_id and d.doc_date >= a.created_on
      GROUP BY 1,3 ORDER BY 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM