2
I need to create a query that I can filter the sales that have issued notes, in my structure there are 2 tables: the sales table and the table nfe. Both are related by the field cod_venda
.
How can I do?
SELECT v.cod_venda
FROM vendas v
JOIN
( SELECT COUNT(num_nfe) AS qnt_notas
, cod_venda
FROM nfe
GROUP
BY nfe.cod_venda
) notas
ON nota.cod_venda = v.cod_venda
WHERE notas.qnt_notas > 0
AND nfe.status in ('emitida', 'cancelada')
Thanks for the answer, but doing the test noticed that this way when there is no corresponding data in the table of notes the query does not return any record, IE, if the sale has no note it will not be shown, would have some idea of how to get around this situation?-
– Victor M.
In place of
JOIN
, placeLEFT JOIN
.– Marcelo Shiniti Uchimura
Although, if the sale doesn’t have notes, it doesn’t make sense to count the notes, it’s not true?
– Marcelo Shiniti Uchimura
I edited the answer.
– Marcelo Shiniti Uchimura
So. the function of this query would be to list sales with notes and without notes, for this I need to make this comparison between the two tables, the difficulty is that some sales may have more than one note, otherwise you could make a trigger and record the note code on the sales chart... I’ll keep trying here, even with the left Join didn’t work, having doesn’t obey the filter.
– Victor M.
Thank you, now it’s worked!
– Victor M.