1
I have two tables and I want all the elements of the first - regardless of the condition of JOIN (which characterizes a LEFT JOIN) - but before that I want to filter the second table. For example:
Table Client:
╔══════════╦═══════╗
║ Cliente ║ Flag ║
╠══════════╬═══════╣
║ A ║ S ║
║ B ║ V ║
║ C ║ L ║
╚══════════╩═══════╝
Entry table:
╔══════════╦═════════╦═══════════╗
║ 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 all Clients regardless of whether there are related items in the Input 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 to do to get the desired result initially?
I appreciate the reply @Elipe-Caputo. I had done the test with this syntax and the time problem also happens. In the original post it was also suggested and the author agreed that return the same result (but my answer below would be more efficient). If you thought the question was good I’d thank you for an UP-VOTE ;)
– Nigini