473,325 Members | 2,308 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,325 software developers and data experts.

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
--------------------------------------------------------
Nov 23 '05 #1
3 4216
Alexander Pucher wrote:
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

Nov 23 '05 #2
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: pg***********@postgresql.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
--------------------------------------------------------

Nov 23 '05 #3
On Fri, 15 Oct 2004, Najib Abi Fadel wrote:
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.
----- Original Message -----
From: Alexander Pucher
To: pg***********@postgresql.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.
_____/ _____/ _/ Co*****@ESI.it

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #4

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

Similar topics

4
by: Erwin Leonardi | last post by:
Hi all, I just start using SQL Server for my project. I have some questions related to set operations. Suppose I have two tables, Table A and Table B, as following. TableA ...
2
by: Joe Gonzalez | last post by:
Is it possible to perform bitwise operations in selects in DB2 8.1? In postgres I can say something like SELECT ... FROM <sometable> WHERE RecFlags && 0x08 <> 0 to get a bitwise and operation. I...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
2
by: rameshadari | last post by:
Hello Guys, I am new to SQL Server Database programming. My specific question is:- Is it possible to do some mathematical operations on columns present in a table. If possible, How? For...
9
by: tomamil | last post by:
imagine that you have different matrices with different names and you want to perform the same action with each of them. is it possible to put their names into some array and to create a loop that...
1
by: tbrogdon | last post by:
I am new to this group and new to DBs. I am building a small DB for my work. We create sheetmetal parts. Each part has a part number (e.g., 1054471 or 50TG508506 - both formats are typical for a...
0
by: ssnsridhar | last post by:
Hi, I ve saw a sample instance provider and have done a service which wil just display wat i have entered in ma service code.. Now i want to Enumerate, Create, Get, Modify operations. Can anyone...
2
by: Madmartigan | last post by:
Hi Operating system is WinXP using SharpDevelop version 1.1.0 and build 2124. I'm new to C# and have a problem trying to get a user to enter 3 mathematical operators of choice, then 2 numbers...
5
by: jacob navia | last post by:
The cephes math library is a legend in the internet. It was written by Stephen L. Moshier in 1984, but there was apparently an even earlier version in assembler, before he rewrote that in C. It is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.