Mysql - search for missing dates with multiple join tables

I need help writing mysql query. I need to find all the missing reports for a specific date range for the project id id.

In principle, for a given business identifier, I need to know the name of the project and all the dates when the report is either missing or not marked as completed.

I use the calendar trick (as described here and here ) to find missing report dates, but I am having problems attaching to the project table to find the related project / business that the report was missed for.

I need a result set that will give me data like this:

+------------+-----------+--------------+
| project_id | name      | missing_date |
+------------+-----------+--------------+
| 1          | Project 1 | 2014-01-01   |
| 1          | Project 1 | 2014-01-03   |
| 1          | Project 1 | 2014-01-04   |
| 1          | Project 1 | 2014-01-07   |
| 1          | Project 1 | 2014-01-09   |
| 2          | Project 2 | 2014-01-02   |
| 2          | Project 2 | 2014-01-03   |
| 2          | Project 2 | 2014-01-04   |
+------------+-----------+--------------+

Here is my diagram:

projects table:
+----------------+------------------+------+-----+-------------------+----------------+
| Field          | Type             | Null | Key | Default           | Extra          |
+----------------+------------------+------+-----+-------------------+----------------+
| project_id     | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| business_id    | int(10) unsigned | NO   | MUL | NULL              |                |
| name           | tinytext         | YES  |     | NULL              |                |
+----------------+------------------+------+-----+-------------------+----------------+

reports table:
+---------------------+------------------+------+-----+-------------------+----------------+
| Field               | Type             | Null | Key | Default           | Extra          |
+---------------------+------------------+------+-----+-------------------+----------------+
| report_id           | int(10) unsigned | NO   | PRI | NULL              | auto_increment |
| project_id          | int(10) unsigned | NO   | MUL | NULL              |                |
| report_date         | date             | NO   | MUL | NULL              |                |
| completed           | bit(1)           | NO   |     | b'0'              |                |
+---------------------+------------------+------+-----+-------------------+----------------+


calendar table:
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| dt           | date        | NO   | PRI | NULL    |       |
| month_name   | varchar(9)  | YES  |     | NULL    |       |
| day_name     | varchar(9)  | YES  |     | NULL    |       |
| y            | smallint(6) | YES  |     | NULL    |       |
| q            | tinyint(4)  | YES  |     | NULL    |       |
| m            | tinyint(4)  | YES  |     | NULL    |       |
| d            | tinyint(4)  | YES  |     | NULL    |       |
| dw           | tinyint(4)  | YES  |     | NULL    |       |
| w            | tinyint(4)  | YES  |     | NULL    |       |
| is_weekday   | bit(1)      | YES  |     | NULL    |       |
| is_holiday   | bit(1)      | YES  |     | NULL    |       |
| holiday_desc | varchar(32) | YES  |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+

, , .

select 
    p.project_id,
    p.name,
    c.dt as missing_date,
    r.completed
from reports r
join projects p on (r.project_id = p.project_id)
right join calendar c on (c.dt = r.report_date)
where c.dt >= '2014-02-01'
and c.dt <= '2014-02-10'
-- and r.report_date is null /** THE RESULT SET IS EMPTY IF I UNCOMMENT THIS **/
and r.completed = false
and c.is_holiday = false
and c.is_weekday = true
and p.business_id = 1001
order by p.project_id, r.report_date, c.dt;

!

+4
2

, , , . , , , - Java, , :). - , !

:

select 
    p.project_id,
    p.name,
    c.dt as missing_date,
    r.report_date,
    r.completed
from calendar c
inner join (
    select 
        p1.project_id,
        p1.name
    from projects p1
    where p1.project_id = 1005
    -- where p1.business_id = 1001 /** OR USE THE BUSINESS ID **/

) p on c.dt between '2014-02-01' and '2014-02-28'
left join reports r on r.report_date = c.dt
and r.project_id = p.project_id
and r.completed = false
where (r.report_date is null or r.completed = false)
and c.is_holiday = false
and c.is_weekday = true
order by p.project_id, c.dt;

:

+------------+--------------+--------------+-------------+-----------+
| project_id | name         | missing_date | report_date | completed |
+------------+--------------+--------------+-------------+-----------+
|       1005 | Project 1005 | 2014-02-03   | 2014-02-03  |         0 |
|       1005 | Project 1005 | 2014-02-04   | 2014-02-04  |         0 |
|       1005 | Project 1005 | 2014-02-05   | NULL        |      NULL |
|       1005 | Project 1005 | 2014-02-06   | 2014-02-06  |         0 |
|       1005 | Project 1005 | 2014-02-07   | NULL        |      NULL |
|       1005 | Project 1005 | 2014-02-10   | 2014-02-10  |         0 |
|       1005 | Project 1005 | 2014-02-11   | 2014-02-11  |         0 |
|       1005 | Project 1005 | 2014-02-12   | 2014-02-12  |         0 |
|       1005 | Project 1005 | 2014-02-13   | NULL        |      NULL |
|       1005 | Project 1005 | 2014-02-14   | NULL        |      NULL |
|       1005 | Project 1005 | 2014-02-18   | NULL        |      NULL |
|       1005 | Project 1005 | 2014-02-19   | NULL        |      NULL |
|       1005 | Project 1005 | 2014-02-20   | 2014-02-20  |         0 |
|       1005 | Project 1005 | 2014-02-21   | 2014-02-21  |         0 |
|       1005 | Project 1005 | 2014-02-24   | 2014-02-24  |         0 |
|       1005 | Project 1005 | 2014-02-25   | 2014-02-25  |         0 |
|       1005 | Project 1005 | 2014-02-26   | NULL        |      NULL |
|       1005 | Project 1005 | 2014-02-27   | NULL        |      NULL |
|       1005 | Project 1005 | 2014-02-28   | NULL        |      NULL |
+------------+--------------+--------------+-------------+-----------+

!

0

, , . , :

right join calendar c on (c.dt = r.report_date)

report_date . , - , , , .

0

Source: https://habr.com/ru/post/1529276/


All Articles