Group by error of a query in db2 SQL

Asked

Viewed 126 times

0

The Consultation:

Select ITEMNFS.Recnum, ITEMNFS.ITEM, sum(ITEMNFS.VL_TOTAL), sum(ITEMNFS.QTDE_FATUR), ITEM.PER_IPI 
from ITEMNFS 
inner join ITEM on ITEMNFS.ITEM = ITEM.ITEM inner join NFS on ((ITEMNFS.NRO_NFS = NFS.NRO_NFS) and (ITEMNFS.SERIE = NFS.SERIE)) 
where (NFS.DT_EMISSAO between '2017-12-01' and '2018-02-20') 
group by ITEMNFS.ITEM

The error is as follows:

SQL0119N An expression started with "PER_IPI" specified in a SELECT or HAVING clause not specified in GROUP BY clause or is in a SELECT, HAVING or ORDER BY clause with a function of column without a specified GROUP BY clause. SQLSTATE=42803

1 answer

1

Error says you need to add all clauses that are not sum() at the group by. Try it this way:

Select ITEMNFS.Recnum, ITEMNFS.ITEM, sum(ITEMNFS.VL_TOTAL), sum(ITEMNFS.QTDE_FATUR), ITEM.PER_IPI 
from ITEMNFS 
inner join ITEM on ITEMNFS.ITEM = ITEM.ITEM inner join NFS on ((ITEMNFS.NRO_NFS = NFS.NRO_NFS) and (ITEMNFS.SERIE = NFS.SERIE)) 
where (NFS.DT_EMISSAO between '2017-12-01' and '2018-02-20') 
group by ITEMNFS.Recnum, ITEMNFS.ITEM, ITEM.PER_IPI 

You can have a read at that link about the group by, there are several examples of selects with many columns.

  • Okay, it works, but not exactly the way I want it. I would need to group and sum them with all lines that have ITEMNFS.ITEM equal I don’t know if I understand

  • For that you would need to remove the other columns (ITEMNFS.Recnum and ITEM.) so much of select how much of group by

Browser other questions tagged

You are not signed in. Login or sign up in order to post.