I have two tables and I want all elements of the first one - regardless of the JOIN condition (which characterizes a LEFT JOIN) - but before that I want to filter the second table. For example:
Client table:
╔══════════╦═══════╗
║ Cliente ║ Flag ║
╠══════════╬═══════╣
║ A ║ S ║
║ B ║ V ║
║ C ║ L ║
╚══════════╩═══════╝
Table Entry:
╔══════════╦═════════╦═══════════╗
║ Cliente ║ Entrada ║ Categoria ║
╠══════════╬═════════╬═══════════╣
║ A ║ 5575 ║ D ║
║ A ║ 6532 ║ C ║
║ A ║ 3215 ║ D ║
║ A ║ 5645 ║ M ║
║ B ║ 3331 ║ A ║
║ B ║ 4445 ║ D ║
╚══════════╩═════════╩═══════════╝
OK. Running a LEFT JOIN I will have every Client regardless of whether there are related items in the Entry table, but before that I want to filter the latter by Category = D - before JOIN.
Desired result:
╔══════════╦═══════╦═════════╗
║ Cliente ║ Flag ║ Entrada ║
╠══════════╬═══════╬═════════╣
║ A ║ S ║ 5575 ║
║ A ║ S ║ 3215 ║
║ B ║ A ║ 4445 ║
║ C ║ L ║ NULL ║
╚══════════╩═══════╩═════════╝
If I use the search below I will lose the last item of the desired result:
SELECT c.Cliente, c.Flag, e.Entrada
FROM Cliente AS c
LEFT JOIN Entrada AS e
ON c.Cliente = e.Cliente
WHERE e.Categoria='D'
How do you get the desired result initially?