473,770 Members | 7,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to calculate difference between 2 columns excluding nulls

Here is my input table:

TUE MON
----------- -----------
2 -
- 25
27 -
- 48
50 -
- 78
3 -
- 40
42 -
- 62
65 -
- 85
4 -
- 40
42 -
- 62
68 -
- 92
What is the best way to calculate difference between not NULL MON and
previous not NULL TUE

25 - 2
48 - 27
........
62 - 40
92 - 68

I did it by creating separated tables for MON and TUE AND THEN JOIN BASED ON
ROW NUMBER
Any more creative solutions.
Thank's in advance
Leny G.

--
Message posted via http://www.dbmonster.com

Jun 27 '08 #1
6 3424
UPS IT SHOULD BE 62 - 42 NOT 40

lenygold wrote:
>Here is my input table:

TUE MON
----------- -----------
2 -
- 25
27 -
- 48
50 -
- 78
3 -
- 40
42 -
- 62
65 -
- 85
4 -
- 40
42 -
- 62
68 -
- 92
What is the best way to calculate difference between not NULL MON and
previous not NULL TUE

25 - 2
48 - 27
........
62 - 42
92 - 68

I did it by creating separated tables for MON and TUE AND THEN JOIN BASED ON
ROW NUMBER
Any more creative solutions.
Thank's in advance
Leny G.
--
Message posted via http://www.dbmonster.com

Jun 27 '08 #2
lenygold via DBMonster.com wrote:
Here is my input table:

TUE MON
----------- -----------
2 -
- 25
27 -
- 48
50 -
- 78
3 -
- 40
42 -
- 62
65 -
- 85
4 -
- 40
42 -
- 62
68 -
- 92
What is the best way to calculate difference between not NULL MON and
previous not NULL TUE
Define "previous".
Remember a table is an _unordered_ set of tuples.

--
Jeroen
Jun 27 '08 #3
if MON is not Null in 2nd row ten Tue Is not null In 1st - previous
MON is not Null is 4th position, Tue is not null in 3rd -previous

The Boss wrote:
>Here is my input table:
[quoted text clipped - 20 lines]
>What is the best way to calculate difference between not NULL MON and
previous not NULL TUE

Define "previous".
Remember a table is an _unordered_ set of tuples.
--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....m-db2/200805/1

Jun 27 '08 #4
lenygold via DBMonster.com wrote:
if MON is not Null in 2nd row ten Tue Is not null In 1st - previous
MON is not Null is 4th position, Tue is not null in 3rd -previous

The Boss wrote:
>>Here is my input table:
[quoted text clipped - 20 lines]
>>What is the best way to calculate difference between not NULL MON and
previous not NULL TUE
Define "previous".
Remember a table is an _unordered_ set of tuples.
OK, first I would merge the two columns into one with COALESCE().
Then use two OLAP functions (e.g. MAX) where one only looks at the
current row and one looks at the preceding row and subtracts them.

Easy as PI ;-)

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Jun 27 '08 #5
I forgot to copy the result:
WITH
Data_sample(n, tue, mon) AS (
VALUES
( 1, 2, CAST(NULL AS INTEGER) )
,( 2, CAST(NULL AS INTEGER), 25 )
,( 3, 27, CAST(NULL AS INTEGER) )
,( 4, CAST(NULL AS INTEGER), 48 )
,( 5, 50, CAST(NULL AS INTEGER) )
,( 6, CAST(NULL AS INTEGER), 78 )
,( 7, 3, CAST(NULL AS INTEGER) )
,( 8, CAST(NULL AS INTEGER), 40 )
,( 9, 42, CAST(NULL AS INTEGER) )
,(10, CAST(NULL AS INTEGER), 62 )
,(11, 65, CAST(NULL AS INTEGER) )
,(12, CAST(NULL AS INTEGER), 85 )
,(13, 4, CAST(NULL AS INTEGER) )
,(14, CAST(NULL AS INTEGER), 40 )
,(15, 42, CAST(NULL AS INTEGER) )
,(16, CAST(NULL AS INTEGER), 62 )
,(17, 68, CAST(NULL AS INTEGER) )
,(18, CAST(NULL AS INTEGER), 92 )
) -- End of sample data
SELECT M.n
, M.mon - T.tue AS difference
, RTRIM(CHAR(M.mo n)) || ' - ' || RTRIM(CHAR(T.tu e)) AS expression
FROM Data_sample M
INNER JOIN
Data_sample T
ON M.mon IS NOT NULL
AND T.n = M.n - 1 -- T is previous of M
AND T.tue IS NOT NULL -- Redundant predicate
;
------------------------------------------------------------------------------

N DIFFERENCE EXPRESSION
----------- ----------- -------------------------
2 23 25 - 2
4 21 48 - 27
6 28 78 - 50
8 37 40 - 3
10 20 62 - 42
12 20 85 - 65
14 36 40 - 4
16 20 62 - 42
18 24 92 - 68

9 record(s) selected.
Jun 27 '08 #6
Thank You very much Tonkuma

Tonkuma wrote:
>I forgot to copy the result:
WITH
Data_sample(n, tue, mon) AS (
VALUES
( 1, 2, CAST(NULL AS INTEGER) )
,( 2, CAST(NULL AS INTEGER), 25 )
,( 3, 27, CAST(NULL AS INTEGER) )
,( 4, CAST(NULL AS INTEGER), 48 )
,( 5, 50, CAST(NULL AS INTEGER) )
,( 6, CAST(NULL AS INTEGER), 78 )
,( 7, 3, CAST(NULL AS INTEGER) )
,( 8, CAST(NULL AS INTEGER), 40 )
,( 9, 42, CAST(NULL AS INTEGER) )
,(10, CAST(NULL AS INTEGER), 62 )
,(11, 65, CAST(NULL AS INTEGER) )
,(12, CAST(NULL AS INTEGER), 85 )
,(13, 4, CAST(NULL AS INTEGER) )
,(14, CAST(NULL AS INTEGER), 40 )
,(15, 42, CAST(NULL AS INTEGER) )
,(16, CAST(NULL AS INTEGER), 62 )
,(17, 68, CAST(NULL AS INTEGER) )
,(18, CAST(NULL AS INTEGER), 92 )
) -- End of sample data
SELECT M.n
, M.mon - T.tue AS difference
, RTRIM(CHAR(M.mo n)) || ' - ' || RTRIM(CHAR(T.tu e)) AS expression
FROM Data_sample M
INNER JOIN
Data_sample T
ON M.mon IS NOT NULL
AND T.n = M.n - 1 -- T is previous of M
AND T.tue IS NOT NULL -- Redundant predicate
;
------------------------------------------------------------------------------

N DIFFERENCE EXPRESSION
----------- ----------- -------------------------
2 23 25 - 2
4 21 48 - 27
6 28 78 - 50
8 37 40 - 3
10 20 62 - 42
12 20 85 - 65
14 36 40 - 4
16 20 62 - 42
18 24 92 - 68

9 record(s) selected.
--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....m-db2/200805/1

Jun 27 '08 #7

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

Similar topics

14
21541
by: delerious | last post by:
I need to determine an element's width and height in pixels (not including padding, border, and margin) in Javascript. The element will not have width or height styles specified. In Mozilla, I know I can use document.defaultView.getComputedStyle() to do this. IE does not support document.defaultView.getComputedStyle(). It supports offsetWidth/offsetHeight, but those include the padding and border. IE also supports...
7
19989
by: sql-db2-dba | last post by:
Does DB2 just fudge it when it is an empty table? Is there a "formula" for average row size when you have variable length records. Or you really have to know what your application is packing into those varchar columns. Bill Leung leungb@aptea.com
5
2696
by: serge | last post by:
How can i enter Default Values of " " to all the columns of type character of all the tables (excluding system tables) and Default Values of 0 of all columns of type numbers. Excluding all primary key columns. Thank you
23
14062
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that didn't work (the timedelta class doesn't have a year accessor). I looked in the docs and the cookbook, but I couldn't find anything, so
13
3329
by: Angus | last post by:
Hello I have a stream of bytes - unsigned char*. But the 'string' may contain embedded nulls. So not like a traditional c string terminated with a null. I need to calculate the length of these arrays but can't use strlen because it just stops counting at the first null it finds. so how to do it? Angus
2
2900
by: ManningFan | last post by:
Go into a table where you have a field that has NULL values. Right- Click on a record with a value in that field and choose "Filter Excluding Selection". Some (or most, or all) of your records where there is a NULL value in that field get eliminated. Without getting crazy (i.e. designing your own bottons and "re- inventing the wheel"), is there a way to make Access stop doing this?
3
14908
by: iceone | last post by:
Hi everybody, I am monitoring a call center and i need to calculate the working hours between the moment the call is answered and the moment the call is closed. i need to calculate the working hours difference (ie excluding nights, sundays, saturdays, thanksgiving, ecc,) i consider as working hours from 09;00 am to 19:30 pm (monday-fryday) (i.e. from fryday 19:00 to monday 09:30 i want the function to accont for one hour) have u...
2
8711
by: rahulae | last post by:
help me with this I'm able to calculate total working days excluding weekends but how to exclude holidays,is there any other way apart from storing all the holidays in some table and not selecting those or else is there any other option
5
24554
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate business days count which is defined as count of days (optionally inclusive in the current implementation) excluding weekend days and holidays. Let us say periods to calculate are stored in table associated with contacts. keyPeriodID -...
0
9592
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
10230
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
10058
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
9870
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...
0
8886
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6678
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
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.