Here's a simple example: sqlfiddle.com/#!9/8087c5/1
I've got the following query:
select
contracts.ID as contractID,
departments.ID as departmentID,
max(invoices.timestamp) as timestamp
from contracts
left join departments on departments.ID = contracts.departmentID and departments.accountingType > 0
left join invoices on contracts.ID = invoices.contractID
group by contractID
having departmentID is not null;
I am expecting to get a result as follows:
+------------+--------------+---------------------+
| contractID | departmentID | timestamp |
+------------+--------------+---------------------+
| 101 | 301 | NULL |
+------------+--------------+---------------------+
| 102 | 302 | NULL |
+------------+--------------+---------------------+
| 103 | 303 | 2020-05-01 11:11:00 |
+------------+--------------+---------------------+
instead I get:
+------------+--------------+---------------------+
| contractID | departmentID | timestamp |
+------------+--------------+---------------------+
| 103 | 303 | 2020-05-01 11:11:00 |
+------------+--------------+---------------------+
I do not understand why the query cuts the NULL Values of the last left joined table. Am I missing something very simple?