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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add row heading to an aggregate query? 1

Status
Not open for further replies.

ViAn

Technical User
May 7, 2003
30
NO
I have this query:
Code:
select sum(param_1) "PARA1", sum(param_2) "PARA2" from (...)
union
select max(param_1), max(param_2) from (...)
resulting in:
Code:
   PARA1                   PARA2
[value of sum(param_1)] [value of sum(param_2)]
[value of max(param_1)] [value of max(param_2)]
Now I want to add row headings, so I get:
Code:
          PARA1                PARA2
Sum:   [value of sum(param_1)] [value of sum(param_2)]
Max:   [value of max(param_1)] [value of max(param_2)]

Can this be done? I have tried to read books and search at the web, but I have obviously been searching at wrong places...

-ViAn-
 
You're very close. Change your union query to look like:

select "Sum" as description, sum(param_1) "PARA1", ...
union
Select "Max" as description, max(param_1),...


Rob
 
It seemed very simple and powerful. But I failed to say that I have a direct query to an Oracle database. This error-message appears:

"sum": invalid identifier

Is there an analogue method in Oracle Sql? It would have helped me a lot!

-ViAn-
 
Put the sum in single quotes:

select 'sum' as description, sum(param_1)...

Also, the 'as' can be left in or taken out.





Rob
 
Why have I spent so much time on this? I did actually find a solution that is highly "un-elegant", making a new table with the words "sum", "avg" etc in separate columns and adding the max() of each of these columns to the "sum" and "avg" parts of the select-query, respectively.

But your way is the right way to do it. A star to you, Rob!

-ViAn-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top