Andy Vincent
Andy Vincent

Reputation: 83

Group with summing in SQL Oracle

in a table below, I need to sum COL3 and COL5, grouped by COL2NAME. So there will be 3 rows:

DC
Cred
Equi

... with summed values of COL3 and COL5.

How could I do it?

Oracle Developer

And here's the query from the screenshot:

select t.*
from (select COL1, COL2 as COL2NAME, sum(COL3A+COL3B) as COL3,
COL4 as COL4NAME, count(COL5NO) as COL5, DENSE_RANK()OVER(ORDER BY COL1) GROUPID
      from TAB
      group by COL1, COL2, COL4
     ) t
where GROUPID = 1
or GROUPID = 2

Upvotes: 0

Views: 45

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269493

So just add a group by:

select col2name, sum(col3 + col5)
from (select COL1, COL2 as COL2NAME, sum(COL3A+COL3B) as COL3,
             COL4 as COL4NAME, count(COL5NO) as COL5,
             DENSE_RANK() OVER (ORDER BY COL1) as GROUPID
      from TAB
      group by COL1, COL2, COL4
     ) t
where GROUPID IN (1, 2)
group by col2name;

Upvotes: 3

Related Questions