SQLite Forum

LEFT JOIN Misunderstanding
Login
Greetings.

The following code is working partially:

```
WITH LastEntries (ProjID, pl_insert, pe_insert)
AS
(
  SELECT projid,
         max(InsertDate),
         (SELECT max(insertdate) FROM project_extras where projid = e.projid)
    FROM project_list e
    GROUP BY projid
 )
SELECT a.ProjID, a.Project_Name, count(c.CreatedDate), a.Manager
FROM Project_List AS a 
LEFT JOIN Project_Extras AS b
LEFT JOIN Project_Highlights AS c
LEFT JOIN LastEntries AS d
WHERE
    c.CreatedDate LIKE '2021-07-%' 
AND
    b.MonthlyRpt = 'y'
AND
    b.BudgetYear = '2021'
AND
    a.InsertDate = d.pl_insert
AND
    b.InsertDate = d.pe_insert
AND a.ProjID = b.ProjID 
AND a.ProjID IS c.ProjID
AND a.ProjID = d.projid
GROUP BY a.ProjID
ORDER BY a.Manager, a.ProjID
;
```

But I am expecting 108 records, and I am only getting 13.  The reason is that the Project_Highlights table does not have all the ProjIDs that Project_List has. However, I am trying to show those ProjIDs in Project_List also with a count of 0. I know what I am missing is simple, but, I have searched a bunch of places trying to understand the reasoning behind it.  I know it's because c.ProjID is NULL, but I am missing the way to have it worked. Any input would be greatly appreciated.  Thanks.