dbTalk Databases Forums  

Mathematical operations with NULL values

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Mathematical operations with NULL values in the comp.databases.postgresql.general forum.



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

Default Mathematical operations with NULL values - 10-15-2004 , 04:18 AM






Hi,

given a table with some data, e.g. some monthly measures. Some of the
measures are missing though.


id m1 m2 m3 m4 m5 .... m12
----------------------------------------------

1 23 45 66 76 76 .... 12
2 76 NULL 77 88 77 ... 89
3 67 87 98 NULL 78 ... NULL

I would like the calculate the yearly average of each row, something
like ((m1+m2+m3+m4+m5+...m12)/12). This would work if I had all montly
values for one year. In the case of at least one NULL value involved, I
would get NULL as result.

So instead of dividing each year by 12, I would have to divide by the
number of measures available in each row.

Could someone point me to the correct SQL syntax for doing this.

Thanks a lot
alex.

--
--------------------------------------------------------
Departement of Geography and Regional Research
University of Vienna
Cartography and GIS
--------------------------------------------------------
Virtual Map Forum: http://www.gis.univie.ac.at/vmf
--------------------------------------------------------



Reply With Quote
  #2  
Old   
Richard Huxton
 
Posts: n/a

Default Re: Mathematical operations with NULL values - 10-15-2004 , 05:23 AM






Alexander Pucher wrote:
Quote:
Hi,

given a table with some data, e.g. some monthly measures. Some of the
measures are missing though.


id m1 m2 m3 m4 m5 .... m12
----------------------------------------------

1 23 45 66 76 76 .... 12 2 76 NULL 77
88 77 ... 89
3 67 87 98 NULL 78 ... NULL

I would like the calculate the yearly average of each row, something
like ((m1+m2+m3+m4+m5+...m12)/12). This would work if I had all montly
values for one year. In the case of at least one NULL value involved, I
would get NULL as result.

So instead of dividing each year by 12, I would have to divide by the
number of measures available in each row.
The "correct answer" is to structure your data differently. If you had a
table:
measures (id, month_num, measurement)
you could then use:
SELECT id, AVG(measurement) FROM measures GROUP BY id
You don't even need nulls any more, just don't record values for those
months you don't know about.

If you can't restructure your table, you'll need to write a procedure
that checks each value in turn for null-ness and calculates accordingly.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster



Reply With Quote
  #3  
Old   
Najib Abi Fadel
 
Posts: n/a

Default Re: Mathematical operations with NULL values - 10-15-2004 , 06:19 AM



You can replace Null values by the and make the defaut Value 0 !

If u can't change the Data in the database you can use the coalesce function which replaces the Null value by zero (or any specified value in the second argument) :

select (coalesce(m1,0) + coalesce(m2,0) + ....... +coalesce(m12,0) ) /12

----- Original Message -----
From: Alexander Pucher
To: pgsql-general (AT) postgresql (DOT) org
Sent: Friday, October 15, 2004 11:18 AM
Subject: [GENERAL] Mathematical operations with NULL values


Hi,

given a table with some data, e.g. some monthly measures. Some of the measures are missing though.


id m1 m2 m3 m4 m5 .... m12
----------------------------------------------

1 23 45 66 76 76 .... 12
2 76 NULL 77 88 77 ... 89
3 67 87 98 NULL 78 ... NULL

I would like the calculate the yearly average of each row, something like((m1+m2+m3+m4+m5+...m12)/12). This would work if I had all montly values for one year. In the case of at least one NULL value involved, I would get NULL as result.

So instead of dividing each year by 12, I would have to divide by the number of measures available in each row.

Could someone point me to the correct SQL syntax for doing this.

Thanks a lot
alex.


--
--------------------------------------------------------
Departement of Geography and Regional Research
University of Vienna
Cartography and GIS
--------------------------------------------------------
Virtual Map Forum: http://www.gis.univie.ac.at/vmf
--------------------------------------------------------


Reply With Quote
  #4  
Old   
Marco Colombo
 
Posts: n/a

Default Re: Mathematical operations with NULL values - 10-15-2004 , 11:21 AM



On Fri, 15 Oct 2004, Najib Abi Fadel wrote:

Quote:
You can replace Null values by the and make the defaut Value 0 !

If u can't change the Data in the database you can use the coalesce function which replaces the Null value by zero (or any specified value in the second argument) :

select (coalesce(m1,0) + coalesce(m2,0) + ....... +coalesce(m12,0) ) /12
That's wrong, you should divide by the number of available measures,
not just by 12. NULL is not 0.

Quote:
----- Original Message -----
From: Alexander Pucher
To: pgsql-general (AT) postgresql (DOT) org
Sent: Friday, October 15, 2004 11:18 AM
Subject: [GENERAL] Mathematical operations with NULL values

[...]
So instead of dividing each year by 12, I would have to divide by the number of measures available in each row.
I can't think of any elegant solution.

As Richard already pointed out, you need either to rearrange your table
or write a procedure. You also may create a table when needed, and drop it
when done.

..TM.
--
____/ ____/ /
/ / / Marco Colombo
___/ ___ / / Technical Manager
/ / / ESI s.r.l.
_____/ _____/ _/ Colombo (AT) ESI (DOT) it

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



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.