I need to create a query that I can filter sales that have issued notes, in my structure there are 2 tables: the sales table and the nfe table. The two are listed by the cod_venda
field.
How can I do this?
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')