# getting a sum of a "Grouped by" query

**URL:** <https://forums.toadworld.com/t/getting-a-sum-of-a-grouped-by-query/38649>\
**Category:** Toad for Oracle & DevOps Toolkit\
**Created:** [November 8, 2016, 9:05am UTC](https://forums.toadworld.com/t/getting-a-sum-of-a-grouped-by-query/38649 "2016-11-08T09:05:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe.Butler](https://avatars.discourse-cdn.com/v4/letter/j/71e660/32.png) [@Joe.Butler](https://forums.toadworld.com/u/Joe.Butler)\
**Post date:** [November 8, 2016, 9:05am UTC](https://forums.toadworld.com/t/getting-a-sum-of-a-grouped-by-query/38649/1 "2016-11-08T09:05:06Z")

</div>

is it possible to sum the totals of a “grouped by” query?

My query syntax is below:

select b.region, count (b.parentcode) from pers a, parentunit b where a.parentcode=b.parentcode and a.empstatus = ‘ACT’ group by b.region;

REGION COUNT(B.PARENTCODE)

* * *

SOUTHERN REGION 3778

NORTHERN REGION 2883

HEADQUARTERS 804

3 rows selected.

---

<div class="post-metadata">

**Author:** ![JohnDorlon](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/johndorlon/32/9997_2.png) [@JohnDorlon](https://forums.toadworld.com/u/JohnDorlon)\
**Post date:** [November 8, 2016, 10:13am UTC](https://forums.toadworld.com/t/getting-a-sum-of-a-grouped-by-query/38649/2 "2016-11-08T10:13:54Z")

</div>

I don’t know if there is a way to get a total as a separate row at the bottom, but you can use analytic functions to give you a running total as you go down the list…

Here are some good examples of analytic functions

[http://www.oracle.com/technetwork/issue-archive/2013/13-mar/o23sql-1906475.html](http://www.oracle.com/technetwork/issue-archive/2013/13-mar/o23sql-1906475.html)

If you can’t make that work in conjunction with COUNT/GROUP BY, you could always move your query up into a sub query.

I haven’t tested this, but I think it will work with your data to give a running total.

with data as  
(select b.region, count (b.parentcode) parentcode\_count  
from pers a, parentunit b  
where a.parentcode=b.parentcode  
and a.empstatus = ‘ACT’  
group by b.region)  
select region, parentcode\_count,  
SUM (parentcode\_count) OVER (ORDER BY region) running\_total  
from data  
order by region;

---

<div class="post-metadata">

**Author:** ![markmolnar](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@markmolnar](https://forums.toadworld.com/u/markmolnar)\
**Post date:** [November 8, 2016, 10:17am UTC](https://forums.toadworld.com/t/getting-a-sum-of-a-grouped-by-query/38649/3 "2016-11-08T10:17:28Z")

</div>

Hey Joe,

Don’t know what the overall goal is, but if you want to get the full sum on a per-row basis, try this using the sum analytic:

select b.region, count (b.parentcode) knt, sum(count (b.parentcode)) over () sum\_of\_count

from pers a, parentunit b

where a.parentcode=b.parentcode and a.empstatus = ‘ACT’

group by b.region;

– you could then wrap this query to get percentages and other useful algorithmic info

Alternately, if you’re doing a quick sql\*plus report (old school, not TOAD!) you can try:

break on report

compute sum of knt on report

select b.region, count (b.parentcode) knt

from pers a, parentunit b

where a.parentcode=b.parentcode and a.empstatus = ‘ACT’

group by b.region;

Good Luck!

---

<div class="post-metadata">

**Author:** ![david.fimble](https://avatars.discourse-cdn.com/v4/letter/d/f475e1/32.png) [@david.fimble](https://forums.toadworld.com/u/david.fimble)\
**Post date:** [November 8, 2016, 10:44am UTC](https://forums.toadworld.com/t/getting-a-sum-of-a-grouped-by-query/38649/4 "2016-11-08T10:44:25Z")

</div>

You can use the CUBE function to get the total. I have included an example below…

David

SELECT TYPE\_CD, SUM(CNT) CNT  
FROM (SELECT 'Balls' TYPE\_CD, 1 CNT FROM DUAL  
UNION ALL  
SELECT 'Jacks' TYPE\_CD, 4 CNT FROM DUAL)  
GROUP BY CUBE(TYPE\_CD)  
ORDER BY 1

---

<div class="post-metadata">

**Author:** ![markmolnar](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@markmolnar](https://forums.toadworld.com/u/markmolnar)\
**Post date:** [November 8, 2016, 11:14am UTC](https://forums.toadworld.com/t/getting-a-sum-of-a-grouped-by-query/38649/5 "2016-11-08T11:14:14Z")

</div>

The “group by cube” option seems to be pretty slick and efficient!
