Unix Technical Forum

sum with null value

This is a discussion on sum with null value within the Oracle Miscellaneous forums, part of the Oracle Database category; --> Hi! I've got problem with sql statement.. in dataset like col1 col2 a 1 b 3 c 5 d ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 11:14 AM
T
 
Posts: n/a
Default sum with null value

Hi! I've got problem with sql statement..

in dataset like
col1 col2
a 1
b 3
c 5
d null
e 2

i must to sum col2, but if 1 single value of this column is null then sum
must result to null, according this i wrote statement:

select sum(col2) from table1

but this query results with 11.

any idea??!!

create table table1 (col1 char(1), col2 number);



insert into table1 values('a',1);

insert into table1 values('b',3);

insert into table1 values('c',5);

insert into table1 values('d',null);

insert into table1 values('e',2);



commit;



select sum(col2) from table1;


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 11:14 AM
Maxim Demenko
 
Posts: n/a
Default Re: sum with null value

T schrieb:
> Hi! I've got problem with sql statement..
>
> in dataset like
> col1 col2
> a 1
> b 3
> c 5
> d null
> e 2
>
> i must to sum col2, but if 1 single value of this column is null then sum
> must result to null, according this i wrote statement:
>
> select sum(col2) from table1
>
> but this query results with 11.
>
> any idea??!!
>
> create table table1 (col1 char(1), col2 number);
>
>
>
> insert into table1 values('a',1);
>
> insert into table1 values('b',3);
>
> insert into table1 values('c',5);
>
> insert into table1 values('d',null);
>
> insert into table1 values('e',2);
>
>
>
> commit;
>
>
>
> select sum(col2) from table1;
>
>


select decode(count(*),count(col2),sum(col2)) from table1;

will give you null if any of values for col2 is null and the sum otherwise.

Best regards

Maxim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 11:14 AM
T
 
Posts: n/a
Default Re: sum with null value


"Maxim Demenko" <mdemenko@arcor.de> wrote in message
news:44f2d28f$0$20028$9b4e6d93@newsspool4.arcor-online.net...
>T schrieb:
>> Hi! I've got problem with sql statement..
>>
>> in dataset like
>> col1 col2
>> a 1
>> b 3
>> c 5
>> d null
>> e 2
>>
>> i must to sum col2, but if 1 single value of this column is null then sum
>> must result to null, according this i wrote statement:
>>
>> select sum(col2) from table1
>>
>> but this query results with 11.
>>
>> any idea??!!
>>
>> create table table1 (col1 char(1), col2 number);
>>
>>
>>
>> insert into table1 values('a',1);
>>
>> insert into table1 values('b',3);
>>
>> insert into table1 values('c',5);
>>
>> insert into table1 values('d',null);
>>
>> insert into table1 values('e',2);
>>
>>
>>
>> commit;
>>
>>
>>
>> select sum(col2) from table1;
>>
>>

>
> select decode(count(*),count(col2),sum(col2)) from table1;
>
> will give you null if any of values for col2 is null and the sum
> otherwise.
>
> Best regards
>
> Maxim




tnx Maxim it's working... i lost count(* ) from my mind ))

also works...

select (case when count(*) = count(col2) then sum(col2) else null end)
from table1

best regards,
Tomo



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:44 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com