-1
I have two tables
customers
id | razao_social |
---|---|
1 | company_1 |
2 | company_2 |
and tasks
id | id_client | tasks | competency |
---|---|---|---|
1 | 1 | '101' | '032021' |
2 | 1 | '102' | '032021' |
3 | 2 | '101' | '032021' |
When I make one join
between these tables, I get a correct result, but it’s NOT what I’m looking for:
select c.razao_social , s.tarefas , s.competencia
from clientes c
join tarefas s on c.id = s.id_cliente
where s.competencia = '032021'
order by s.id_cliente;
Result obtained:
razao_social | tasks | competency |
---|---|---|
company_1 | '101' | '032021' |
company_1 | '102' | '032021' |
company_2 | '101' | '032021' |
Result I would like to get:
razao_social | tasks | competency |
---|---|---|
company_1 | '101' | '032021' |
company_1 | '102' | '032021' |
company_2 | '101' | '032021' |
company_2 | null or empty | '032021' |
(or anything else indicating that the task '102' was not made for company_2)
Is there a query that helps me generate this table result? I can (and am) using the programming language to correct this "missing line", but I wanted to know if there is another way, at query level SQL
.
Tested: apply left join
or right join
were insufficient to do what I want.
it is difficult to do with a simple query that, pq your template does not require a task to be done by more than one client
– Ricardo Pontual
Related: What is the difference between INNER JOIN and OUTER JOIN?
– Icaro Martins