Unix Technical Forum

help for operation with date

This is a discussion on help for operation with date within the DB2 forums, part of the Database Server Software category; --> Hi all, I've a table like this: date, value 01/01/2008, 3 01/02/2008, 4 02/03/2008, 7 and I wish to ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-04-2008, 07:23 AM
DM
 
Posts: n/a
Default help for operation with date

Hi all,
I've a table like this:
date, value
01/01/2008, 3
01/02/2008, 4
02/03/2008, 7

and I wish to find with one query the difference (in days) from all
the consecutive intervals (01/02 - 01/01 and 02/03 - 01/02 in this
example).

Someone could help me please?
Thanks all,
DM
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-04-2008, 07:23 AM
Serge Rielau
 
Posts: n/a
Default Re: help for operation with date

DM wrote:
> Hi all,
> I've a table like this:
> date, value
> 01/01/2008, 3
> 01/02/2008, 4
> 02/03/2008, 7
>
> and I wish to find with one query the difference (in days) from all
> the consecutive intervals (01/02 - 01/01 and 02/03 - 01/02 in this
> example).
>
> Someone could help me please?
> Thanks all,
> DM

Take a look at the OLAP functions. Especially the ROWS PRECEEDING
clause. They allow you to dip into trailing or leading rows.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-06-2008, 03:04 PM
--CELKO--
 
Posts: n/a
Default Re: help for operation with date

>> I've a table like this: <<

Please post DDL and do not use reserved words for column names. Let's
cleanup what you have now:

CREATE TABLE Foobar
(foo_date DATE NOT NULL PRIMARY KEY, -- wild guess
bar_value INTEGER NOT NULL);

Also, Standard SQL allows only "yyyy-mm-dd" format for dates. This is
also true for many other ANSI/ISO standards, so you might want to pick
up a good programming habit now. What you posted is ambiguous -- is it
traditional US or UK format?

>> I wish to find with one query the difference (in days) from all the consecutive intervals (01/02 - 01/01 and 02/03 - 01/02 in this example). <<


There are some solutions with self-joins, but the real answer is that
if you want those intervals, you should design your table correctly.
Time is a continuum, so you need to show the days that a "bar_value"
was valid.

CREATE TABLE Foobar
(foo_start_date DATE NOT NULL,
foo_end_date DATE -- null is current
CHECK (foo_start_date <= foo_end_date),
PRIMARY KEY (foo_start_date),
bar_value INTEGER NOT NULL);

I have to guess at when intervals end -- on the next day or before?

INSERT INTO Foobar
VALUES ('2008-01-01', '2008-01-01', 3),
('2008-01-02', '2008-02-02', 4),
('2008-02-03', NULL, 7 );

Here is the SQL-99 code; you might have to change it a bit:

SELECT foo_start_date, bar_value,
(COALESCE (foo_end_date, CURRENT_DATE) - (foo_start_date +
INTERVAL '1' DAY)) DAY
AS bar_duration
FROM Foobar;

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:20 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com