Home > Mobile >  How to display the ID value in a SUM SQL query?
How to display the ID value in a SUM SQL query?

Time:11-23

I have this select query, that summarize columns:

SELECT 
SUM (LineTotal),
SUM (VatSum), 
SUM (GTotal)
FROM CSI1
WHERE DocEntry = '100'

It shows the expected values correctly, but I need to display the id (DocEntry) value too.

Thanks for any advice!

CodePudding user response:

You need a GROUP BY clause:

SELECT 
   DocEntry,
   SUM(LineTotal) AS LineTotal,
   SUM(VatSum) AS VatSum,  
   SUM(GTotal) AS GTotal
FROM (VALUES
   ('100', 1, 1, 1),
   ('100', 1, 1, 1),
   ('200', 1, 1, 1)
) CSI1 (DocEntry, LineTotal, VatSum, GTotal)
WHERE DocEntry = '100'
GROUP BY DocEntry

Result:

DocEntry LineTotal VatSum GTotal
--------------------------------
100      2         2      2
  • Related