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

Please explain query intervals output

I found this query on older thread and i can not uderstand output interval
pairs:

How to find min and max values in date intervals:
--------------------------------------------------
Input:
CREATE TABLE INTERVALS
(key CHAR(5) NOT NULL
,level CHAR(7) NOT NULL
,date_from DATE NOT NULL
,date_to DATE NOT NULL
);

INSERT INTO INTERVALS
VALUES
('key1', 'level1', '2001-05-01', '2002-10-01')
,('key1', 'level1', '2001-01-01', '2001-04-30')
,('key1', 'level1', '2000-11-10', '2000-12-31')
,('key1', 'level1', '2000-06-01', '2000-10-09')
,('key1', 'level1', '2000-01-01', '2000-05-31')
;

SELECT l.key, l.level, l.date_from, r.date_to
FROM Intervals l
Intervals r,
WHERE NOT EXISTS
(SELECT *
FROM Intervals le
WHERE le.date_to = l.date_from - 1 DAY)
AND r.date_to =
(SELECT MIN(date_to)
FROM Intervals rm
WHERE rm.date_to l.date_from
AND NOT EXISTS
(SELECT *
FROM Intervals re
WHERE re.date_from = rm.date_to + 1 DAY));

or

SELECT l.key, l.level, l.date_from
, (SELECT MIN(date_to)
FROM Intervals rm
WHERE rm.date_to l.date_from
AND NOT EXISTS
(SELECT *
FROM Intervals re
WHERE re.date_from = rm.date_to + 1 DAY)
) AS date_to
FROM Intervals l
WHERE NOT EXISTS
(SELECT *
FROM Intervals le
WHERE le.date_to = l.date_from - 1 DAY);

Same output:

KEY LEVEL DATE_FROM DATE_TO
----- ------- ---------- ----------
key1 level1 2000-11-10 2002-10-01
key1 level1 2000-01-01 2000-10-09

2 record(s) selected.
What is the meaning of this output;
The min value in intervals are
2001-01-01
The max value in intervals are
2002-10-01
Please help
Thank's

--
Message posted via DBMonster.com
http://www.dbmonster.com/Uwe/Forums....m-db2/200810/1

Oct 3 '08 #1
1 1761
On 3 Okt, 14:42, "lenygold via DBMonster.com" <u41482@uwewrote:
[...]
>
INSERT INTO INTERVALS
VALUES
*('key1', 'level1', '2001-05-01', '2002-10-01')
,('key1', 'level1', '2001-01-01', '2001-04-30')
,('key1', 'level1', '2000-11-10', '2000-12-31')
,('key1', 'level1', '2000-06-01', '2000-10-09')
,('key1', 'level1', '2000-01-01', '2000-05-31')
;
[...]
KEY * LEVEL * DATE_FROM *DATE_TO * *
----- ------- ---------- ----------
key1 *level1 *2000-11-10 2002-10-01
key1 *level1 *2000-01-01 2000-10-09

*2 record(s) selected.
What is the meaning of this output;
The min value in intervals are
2001-01-01
The max value in intervals are
2002-10-01
Please help
Thank's
Not sure what you find confusing, but I think it will become clear to
you if you draw a timeline from '2000-01-01' to '2002-10-01' and then
put the intervals in the input ontop of that:

00-01-01..00-05-31
00-06-01..00-10-09
<--
00-11-10..00-12-31
01-01-01..

/Lennart
Oct 3 '08 #2

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

Similar topics

6
by: KULJEET | last post by:
i have a table with following data qty start_no end_no 1 1 100 1 101 200 1 201 300 1 5001 5100 1 7001 7100 1 7101 7200 i used query like
0
by: Brady Orand | last post by:
I have a table with the following description: user varchar(64) outtime datetime intime datetime <other irrelevant columns> I want to query this table from datetime1 to datetime2 at...
9
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a...
5
by: jag | last post by:
Hi i have a drill hole geology table with information as follows holeid depthfrom depth to plot1 hwrc1 0 1 cuf hwrc1 1 ...
2
by: ormy28 | last post by:
I really need some help with the following problem if anyone would be willing. I need a list box to list the opposite of what appears in a query. Heres the details: My database is for a...
9
by: colin.mcnulty | last post by:
Hi, I'm a SQL Server DBA, but I guess that won't buy me any friends round here huh? ;-) I've been asked to look at the SQL that's being executed on a DB2 database from a web app, specifically...
10
by: Bishman | last post by:
Hi, I have a form application that needs to query a DB at a set timed interval and then refresh the form with values received from the DB. Simple enough. My design releated questions are .......
0
by: plaidthermos | last post by:
hello, i have a database where there is a main table, documents, and then a relationship table doc_topics with one entry for each topic into which a document falls. (any document may have an...
5
by: kabotnet | last post by:
Hi, I'm new in db2, I'm trying to execute EXPLAIN command on some queries but i have error like: And message similar to: Token EXPLAIN is not valid, valid tokens ( END GET SET CALL DROP FREE...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.