473,425 Members | 1,660 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Getting Avg to really ignore null values

Using SQL2000. According to Books Online, the avg aggregrate function
ignores null values. ((3+3+3+3+Null)/5) predictably returns Null. Is
there a function to ignore the Null entry, adjust the divisor, and
return a value of 3? For example:((3+3+3+3)/4) after ignoring Null
entry.

If there's more than one null value, then adjust divisor accordingly.
For example: ((5+5+5+4+Null+5+5+Null)/8) would be ((5+5+5+4+5+5)/6)
after nulls ignored.

Thanks for any help or advice.

Sep 2 '05 #1
8 12507
The AVG function really does ignore NULL values. It behaves exactly the
way you say you want:

CREATE TABLE T (x INTEGER NULL /* ... */) ;

INSERT INTO T VALUES (3) ;
INSERT INTO T VALUES (3) ;
INSERT INTO T VALUES (3) ;
INSERT INTO T VALUES (3) ;
INSERT INTO T VALUES (NULL) ;

SELECT AVG(x) FROM T ;

Result:

-----------
3

(1 row(s) affected)

I don't understand what your example is supposed to illustrate because
you've only used literals. Are those values supposed to represent
scalar variables? In which case you could do something such as this:

SELECT
(COALESCE(@v1,0)
+COALESCE(@v2,0)
+COALESCE(@v3,0)
+COALESCE(@v4,0)
+COALESCE(@v5,0))
/(CASE WHEN @v1 IS NULL THEN 0 ELSE 1 END+
CASE WHEN @v2 IS NULL THEN 0 ELSE 1 END+
CASE WHEN @v3 IS NULL THEN 0 ELSE 1 END+
CASE WHEN @v4 IS NULL THEN 0 ELSE 1 END+
CASE WHEN @v5 IS NULL THEN 0 ELSE 1 END)

--
David Portas
SQL Server MVP
--

Sep 2 '05 #2
ma**********@hotmail.com wrote:
Using SQL2000. According to Books Online, the avg aggregrate function
ignores null values. ((3+3+3+3+Null)/5) predictably returns Null. Is
there a function to ignore the Null entry, adjust the divisor, and
return a value of 3? For example:((3+3+3+3)/4) after ignoring Null
entry.

If there's more than one null value, then adjust divisor accordingly.
For example: ((5+5+5+4+Null+5+5+Null)/8) would be ((5+5+5+4+5+5)/6)
after nulls ignored.

Thanks for any help or advice.


Works for me:
create table t1 (
name varchar(20),
val int)

insert into t1 values ('f1', 1)
insert into t1 values ('f2', 2)
insert into t1 values ('f3', 3)

select * from t1

select avg(val) as [avg]
from t1

insert into t1 values ('f4', NULL)

select * from t1

select avg(val) as [avg]
from t1

drop table t1
Output
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)

name val
-------------------- -----------
f1 1
f2 2
f3 3

(3 row(s) affected)

avg
-----------
2

(1 row(s) affected)
(1 row(s) affected)

name val
-------------------- -----------
f1 1
f2 2
f3 3
f4 NULL

(4 row(s) affected)

avg
-----------
2

(1 row(s) affected)

Warnung: NULL-Wert wird durch eine Aggregat- oder eine andere
SET-Operation gelöscht.
What exactly is your problem?

Cheers

robert

Sep 2 '05 #3
<ma**********@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Using SQL2000. According to Books Online, the avg aggregrate function
ignores null values. ((3+3+3+3+Null)/5) predictably returns Null. Is
there a function to ignore the Null entry, adjust the divisor, and
return a value of 3? For example:((3+3+3+3)/4) after ignoring Null
entry.

If there's more than one null value, then adjust divisor accordingly.
For example: ((5+5+5+4+Null+5+5+Null)/8) would be ((5+5+5+4+5+5)/6)
after nulls ignored.

Thanks for any help or advice.


Why not simply change your "where" clause so you aren't picking up null
values?

Brian.

--
www.cryer.co.uk/brian
Sep 2 '05 #4
OK, maybe I have my avg function syntax wrong. Given the following set
of values I get a Null result:

<column names>
systems1 systems2 systems3 systems4 systems5
2 2 2 NULL NULL
3 4 5 5 NULL
4 1 2 4 NULL

select avg(systems1+systems2+systems3+systems4+systems5) from
tblEvaluations

All columns are tinyint

Sep 2 '05 #5
ma**********@hotmail.com wrote:
OK, maybe I have my avg function syntax wrong. Given the following
set of values I get a Null result:

<column names>
systems1 systems2 systems3 systems4 systems5
2 2 2 NULL NULL
3 4 5 5 NULL
4 1 2 4 NULL

select avg(systems1+systems2+systems3+systems4+systems5) from
tblEvaluations


select
avg(ISNULL(systems1,0)+ISNULL(systems2,0)+ISNULL(s ystems3,0)+ISNULL(system
s4,0)+ISNULL(systems5,0)) from
tblEvaluations

robert

Sep 2 '05 #6
Try:

SELECT
AVG(COALESCE(systems1,0)
+COALESCE(systems2,0)
+COALESCE(systems3,0)
+COALESCE(systems4,0)
+COALESCE(systems5,0))
FROM tblEvaluations

Note however, that this is the Average of the row totals, not of the
individual values.

--
David Portas
SQL Server MVP
--

Sep 2 '05 #7
Thanks, David & Robert. I believe that's what I needed.

Sep 2 '05 #8
Thanks to everyone one that posted. David, I believe that's what I
needed.

Sep 2 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: gimme_this_gimme_that | last post by:
Hi, This is sort of a : How to build a Yes/No dialog box qquestion. Or perhaps a question about getting javascript variables from a pop-up window and processing them on a submit. This is...
3
by: Ilja Booij | last post by:
Hi all, I have some trouble with the following: I'm getting a time string, in YYYY-MM-DD HH:mm:ss format, which I need to translate into a string with DD-Mon-YYYY HH:mm:ss +HHMM, where the...
10
by: Bonj | last post by:
Hello. I hope somebody can help me on this, because I'm running out of options to turn to. I have almost solved my regular expression function. Basically it works OK if unicode is defined. It...
4
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
7
by: Aaron Gray | last post by:
I put together the following code to get the href's parameters :- function GetParameters() { var arg = new Object(); var href = document.location.href; if ( href.indexOf( "?") != -1) { var...
2
by: Nandarangu | last post by:
Sample data: C_STATUS C_PHONE ---------- ---------- ACTIVE 7044649707 ACTIVE 7044640709 ACTIVE 7044649710 PENDING 7044649701 PENDING 7044649702 WORKING 3015805111
4
bugboy
by: bugboy | last post by:
I'm inserting a new word into table 'w' and a definition into table 'c' which are linked in table 's' which is the relation table for the many to many relationship between 'w' and 'c'. I've been...
9
by: KDawg44 | last post by:
Hi, I have PHP function that adds a record to the database. The table has an ID that is AUTO_INCREMENT. Is there anyway to get that ID back when I do any kind of insert? That ID is a foreign...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.