Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Summary of a summary possible?

Status
Not open for further replies.

MCubitt

Programmer
Mar 14, 2002
1,081
GB
In a single SQL statement, can one provide a GRAND total of grouped by totals?

EG.

select
part, sum(qty)
from
partsinstock
group by part

produces:
Part Sum(qty)
A 100
B 20
C 100
D 80

and I want
Part Sum(qty)
A 100
B 20
C 100
D 80
TOTAL 300

Possible? Please!




Applications Support
UK
 
Try this

select
part, sum(qty)
from
partsinstock
group by part
UNION
select
'TOTAL' part, sum(qty)
from
partsinstock
 
Possible with analytics:
Code:
select
 part, sum(qty) 
from
 partsinstock
group by rollup(part)

Stefan
 
Although I prefer the simplicity of Stefan's suggestion, if you wish to leverage SQL*Plus's built-in report formatting capabilities, you can also say:
Code:
col x heading "Total|Quantities" format 999,999
break on report
compute sum of x on report
select part, sum(qty) x
  from partsinstock
 group by part;

Let us know your eventual preferences.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
@ 18:01 (05Jan05) UTC (aka "GMT" and "Zulu"),
@ 11:01 (05Jan05) Mountain Time

Click here to Donate to Tsunami Relief. 100% of your contributions here go to the victims...0% to administration.
They were "The First-Responder" to the disaster, with relief deliveries arriving before Red Cross and U.S. aid.
 
Thanks you all. Since we wanted a single SQL statement to do it, the first 2 statements do the job.

I am unsure of any benefits of one over the other so assume either will do.

Thanks yee three wise men!



Applications Support
UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top