Skip to content
Advertisement

ORACLE SQL Combining many rows to one and sum a field

ORACLE SQL … I am trying to combine rows into one when they share the same contents in the following fields:

In addition, I need the sum of the fields GL_ACCOUNT_BALANCE from all the respective previous rows to be totaled onto that one new row.

I have written the following code.

Advertisement

Answer

From my point of view, code you posted is too complex. You should have posted something simpler.

Anyway: if I understood you correctly, you’re looking for LISTAGG function which “aggregates” column values (lists them, one after another, in a single column), along with the SUM function (to calculate balance summary).

Sample code:

As you can see, columns that aren’t aggregated have to be listed in the GROUP BY clause.


A simple example, based on Scott’s EMP table:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement