Solved

SQL select sum() ...ignoring null's

Posted on 2003-10-28
7
2,223 Views
Last Modified: 2012-06-21
i have a table test1 (a int)
there are following values:
1
2
3
4
null

now i want select the sum of the column a, i do:
select sum(case when a is null then 0 end) from test1

should be 10 ..but it doesnt run how i specified. i want that the null is ignored. how do i do that?

also i want calculate the average of the table above. there are 4 times a value set, (1+2+3+4) / 4 = 2.5

thx a lot
0
Comment
Question by:soci28
7 Comments
 

Assisted Solution

by:AndrewKT
AndrewKT earned 50 total points
ID: 9633713
Your logic is missing a small item:

create table #test (a int null)

insert into #Test values (1)
insert into #Test values (2)
insert into #Test values (3)
insert into #Test values (4)
insert into #Test values (null)

select sum(case when a is null then 0 else a end) from #test


You gave the sum function a value only when a is null.
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 50 total points
ID: 9633717
you don't need the case

select sum(a.int),avg(a.int)


will ignore the null values  in the columns from the calculations automatically..
0
 
LVL 18

Accepted Solution

by:
ShogunWade earned 25 total points
ID: 9633718
SELECT avg(a),sum(a) from test1
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 18

Expert Comment

by:ShogunWade
ID: 9633727
Pipped at the post again by lowfat ;)
0
 

Author Comment

by:soci28
ID: 9633869
is there a way to find out the average, if the table has more columns than one?

table test1 (a int, b int, c int, d int)
a  b  c  d
2  4  9  null

==> average should be 5 .. how do i find that?
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9633948
select (isnull(a,0)+isnull(b,0)+isnull(c,0)+isnull(d,0)) /
         ( case when a is null 0 else 1 end+case when b is null 0 else 1 end+case when c is null 0 else 1 end+case when d is null 0 else 1 end)
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 9639082
shogun's solution is the only way for sql server although i believe oracle has
function which work "on" a row..
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now