Without the knowledge about your data model, I don't know. The fact is,
your second query incorporates Articoli and if there are some rows in
doc_corpo, which don't have corresponding rows in Articoli, those won't
be taken into account in the second query, but will be processed in the
first one. So check for that - see if you've got dc_corpo without
corresponding Articoli (null Articolo_id or a value not matching any
Articoli.id).
Try to transform your first query to use Articoli like the second one
(but without grouping) - I bet the sum will be different.
If you need the grouping by Articoli and still want to sum doc_corpo
without corresponding Articoli, you'll need to use left join, something
like:
select
sum(iif(td.partite = 1, dc.Importo * 1, dc.Importo * -1)) as Compenso,
a.ss_campo
from
doc_testa dt join doc_corpo dc on (
dt.id = dc.doc_testa_id
and (extract (year from dt.data) = 2025)
and dc.ritenuta = 1
) join Tipo_Documenti td on (
dt.documento_id =
td.id
and td.Partite > 0
) left join Articoli a on (
dc.Articolo_id =
a.id
)
group by a.ss_campo
For doc_corpo without matching Articoli you'll have null-labeled group.
regards
Tomasz