Salve ho due tabelle in cui dovrei incrociare dei dati per prelevare la fattura (presente in una delle due tabelle) e mostrare le righe :

SELECT id,id_prodotti
FROM ddt_righe_sparate
where id_ddt=2
and deleted_at is null

che restituisce : https://imgur.com/E5Luf7w.png



SELECT id,fattura,id_prodotti FROM ddt_righe_inserite
where id_ddt=2
and deleted_at is null

che restituisce : https://imgur.com/d7T2Gly.png


quindi ho creato una query per fare la join tra le due

select
ddt_righe_ins.fattura,
prodotti.codice_articolo,
prodotti.descrizione,
prodotti.id,
count(ddt_righe_sparate.id_prodotti) as num_prodotti
from ddt_righe_sparate
left join (SELECT * FROM ddt_righe_inserite
where id_ddt=2
and deleted_at is null)as ddt_righe_ins on ddt_righe_ins.id_prodotti=ddt_righe_sparate.id_pro dotti
join prodotti on prodotti.id=ddt_righe_sparate.id_prodotti
where ddt_righe_sparate.id_ddt=2
group by
ddt_righe_ins.fattura,
prodotti.codice_articolo,
prodotti.descrizione,
prodotti.id

che restituisce : https://imgur.com/RPoPMHE.png

mi aspetterei correttamente tre righe ma quello che non capisco è come la riga con id 313 abbia 4 come num_prodotti e non 2