SQLite Forum

LEFT JOIN Misunderstanding
Login
```
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 AS e
  GROUP BY projid)

SELECT count(d.CreatedDate) AS Jul, count(c.CreatedDate) AS Aug, a.Project_Type, a.ProjID
  FROM Project_List AS a
  LEFT JOIN Project_Extras AS b ON a.ProjID = b.ProjID AND b.FinCarryOver <> 'y' AND b.MonthlyRpt = 'y' AND b.BudgetYear = '2021'
  LEFT JOIN Project_Highlights AS c ON b.ProjID = c.ProjID AND c.CreatedDate LIKE '2021-08%'
  LEFT JOIN Project_Highlights AS d ON b.ProjID = d.ProjID AND d.CreatedDate LIKE '2021-07%'
  LEFT JOIN LastEntries AS f
  WHERE a.Project_Delivered <> 'Yes' AND 
        a.PMO_Board_Report <> 'No' AND 
        a.Status = 'Acknowledged' AND 
        a.InsertDate = f.pl_insert AND 
        b.InsertDate = f.pe_insert AND 
        a.ProjID = f.projid
GROUP BY a.ProjID
ORDER BY a.Manager, a.ProjID;
```

You have

```
SELECT count(d.CreatedDate) AS Jul, count(c.CreatedDate) AS Aug, a.Project_Type, a.ProjID
```

But 

```
GROUP BY a.ProjID
```

That's going to give you weird results.  The select line should really be only group by columns or aggregates.  Maybe add 

```
GROUP BY a.ProjID, a.Project_Type
```

And as before

```
        a.InsertDate = f.pl_insert AND 
        b.InsertDate = f.pe_insert AND 
        a.ProjID = f.projid
```

is killing the `LEFT` in `LEFT JOIN LastEntries AS f` 

Those probably need to move into an `ON` clause for `f` unless you intended that to be an inner join.