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 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
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
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
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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
|
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
|
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
| |
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?
|
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...
|
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
|
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 -...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |