Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to get subtotals and totals of multiple columns?

marindoSep 11 2015 — edited Sep 11 2015

Hi!

I have a table like this:

SportWomanMan
Soccer50200
Volleyball12080
Tennis140100
Running350200


Now I want

(a) add a column "Ratio" showing the value of Woman/Men.

(b) add row showing the totals. Like this:

Sport
WomenMan
Ratio
Soccer502000.25
Volleyball120801.50
Tennis1401001.40
Running3502001.75
TOTAL6605801.14

Consider the case the initial table is created form database like this:

create table member (

name varchar2(99),

gender char(1),

sport varchar2(99)

);

Would it be possible to create the 2nd table in one go though the member table by a SQL query? I.e. by creating subtotals per sport and adding the ration per sport and finally adding the totals? I know how to do (a), but only for the original rows. I know how to calculate the totals. But this would end up in to SQL querys, one for the subtotals and one for the totals, which does not seem to be efficient...

Any pointers welcome!

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 9 2015
Added on Sep 11 2015
11 comments
3,242 views