473,657 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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+m 5+...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 4240
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+m 5+...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***********@p ostgresql.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***********@p ostgresql.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*******@postg resql.org

Nov 23 '05 #4

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

Similar topics

4
22676
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 TableB ======= ======= --------------- --------------- | ID | DATA | | ID | DATA |
2
7526
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 can't find anything similar in the db2 SELECT docs. Is there a built-in function that can perform the bitwise operation? Thanks in advance for any info. Joe Gonzalez
17
4653
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 analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if( someString.ToLower() == "somestring" ) and
2
3593
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 example, how can the following operation be performed:- (25*Column3) - (2*Column1) Please kindly let me know
9
2128
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 would call their names right from the array and perform the desired operations with the members of the matrices?
1
1808
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 part number). Any given part may have a number of operatons involved in it's production. Each operation process has its own allotted code: shear - 1000; CNC machine - 2000; stamping press - 3000; etc. Any given part may also have multiple...
0
1590
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 plz help me out.. I have included ma code in this.. plz tel how to implement those operations.. using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics;
2
2392
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 of choice, and giving ouput for each of the mathematical operations using the 2 numbers. The general script will work but I have a user inputting a string which I don't know how to convert to int to be capable of using it in mathematical...
5
4496
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 used in many systems, for instance in the python language. I started working with this library around 1997 or whereabouts. I rewrote the core of it in assembler, fixed some (minor) bugs, and added several functions like the Lambert's W...
0
8411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.