dbTalk Databases Forums  

two total from a Select

comp.databases.oracle.server comp.databases.oracle.server


Discuss two total from a Select in the comp.databases.oracle.server forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
valigula
 
Posts: n/a

Default two total from a Select - 05-21-2007 , 04:28 AM






I need to calculate two differents totals from a table . One is
filtering by the year to know how many from this year and the other is
a total without that filter.
i am using oracle 9i. At the moment i am trying to use the rollup
function.
Is it more apropiate to use a subquery???

thanks

A


Reply With Quote
  #2  
Old   
ErikYkema
 
Posts: n/a

Default Re: two total from a Select - 05-23-2007 , 02:18 PM






On May 21, 11:28 am, valigula <valig... (AT) gmail (DOT) com> wrote:
Quote:
I need to calculate two differents totals from a table . One is
filtering by the year to know how many from this year and the other is
a total without that filter.
i am using oracle 9i. At the moment i am trying to use the rollup
function.
Is it more apropiate to use a subquery???

thanks

A
Hi,
Sum will work over nulls, so if you replace values that do not comply
with the filter condition with null, you can get your requirement
fulfilled, assuming I have understood it correctly.

select object_type
, sum(object_id) as total_without_filter
, sum(
case
when not (object_type = 'TABLE') then null
else object_id
end
) as total_with_filter
from all_objects
group by object_type

(Of course, summing over object_id is non sensical and selecting from
all_objects not very elegant.)
Good luck and please come back when you need more help.
Regards,
Erik Ykema



Reply With Quote
  #3  
Old   
valigula
 
Posts: n/a

Default Re: two total from a Select - 05-25-2007 , 02:52 AM



On 23 mayo, 21:18, ErikYkema <erik.yk... (AT) gmail (DOT) com> wrote:
Quote:
On May 21, 11:28 am,valigula<valig... (AT) gmail (DOT) com> wrote:

I need to calculate two differents totals from a table . One is
filtering by the year to know how many from this year and the other is
a total without that filter.
i am using oracle 9i. At the moment i am trying to use the rollup
function.
Is it more apropiate to use a subquery???

thanks

A

Hi,
Sum will work over nulls, so if you replace values that do not comply
with the filter condition with null, you can get your requirement
fulfilled, assuming I have understood it correctly.

select object_type
, sum(object_id) as total_without_filter
, sum(
case
when not (object_type = 'TABLE') then null
else object_id
end
) as total_with_filter
from all_objects
group by object_type

(Of course, summing over object_id is non sensical and selecting from
all_objects not very elegant.)
Good luck and please come back when you need more help.
Regards,
Erik Ykema
Thanks for your answer .. i managed to do it using ...
......
COUNT (*) total1,
COUNT (DECODE (table1_filtered.anyoalta, year, year)) total2,
....
I would have a look to tour sentence as well . ;O)
ty

A.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.