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

Selecting Next Value Only...

I have a table with a number of fields, one being a date.

What I'm looking to do is take a date variable, and then select only
one row - that being the one that contains the next date value in
chronological order. So, as an example, if the table contains

1. 2005-12-01
2. 2006-03-01
3. 2006-06-01
4. 2006-09-01
5. 2006-12-01
6. 2007-03-01
7. 2007-06-01
8. 2007-09-01

I want to run the query and enter the date 2006-10-09, and have the
result returned

1. 2006-12-01

Oct 9 '06 #1
3 1679
th*******@gmail.com wrote:
I have a table with a number of fields, one being a date.

What I'm looking to do is take a date variable, and then select only
one row - that being the one that contains the next date value in
chronological order. So, as an example, if the table contains

1. 2005-12-01
2. 2006-03-01
3. 2006-06-01
4. 2006-09-01
5. 2006-12-01
6. 2007-03-01
7. 2007-06-01
8. 2007-09-01

I want to run the query and enter the date 2006-10-09, and have the
result returned

1. 2006-12-01
SELECT * FROM T
WHERE COLDATE ?
ORDER BY COLDATE
FETCH FIRST ROW ONLY

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Oct 9 '06 #2
That's great Serge- it works :). But, now it's got a little more
complicated...

Imagine in the table

contract date
1. CONTRACT A 2005-12-01
2. CONTRACT B 2006-03-01
3. CONTRACT C 2006-06-01
4. CONTRACT A 2006-09-01
5. CONTRACT B 2006-12-01
6. CONTRACT C 2007-03-01
7. CONTRACT A 2007-06-01
8. CONTRACT B 2007-09-01
9. CONTRACT C 2007-12-01

This time, I'm trying to select the first option after the current
date, but for each contract. So if I run the query with current date as
2006-10-09 the return would be

1. CONTRACT B 2006-12-01
2. CONTRACT C 2007-03-01
3. CONTRACT A 2007-06-01

Serge Rielau wrote:
th*******@gmail.com wrote:
I have a table with a number of fields, one being a date.

What I'm looking to do is take a date variable, and then select only
one row - that being the one that contains the next date value in
chronological order. So, as an example, if the table contains

1. 2005-12-01
2. 2006-03-01
3. 2006-06-01
4. 2006-09-01
5. 2006-12-01
6. 2007-03-01
7. 2007-06-01
8. 2007-09-01

I want to run the query and enter the date 2006-10-09, and have the
result returned

1. 2006-12-01
SELECT * FROM T
WHERE COLDATE ?
ORDER BY COLDATE
FETCH FIRST ROW ONLY

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Oct 9 '06 #3
th*******@gmail.com wrote:
That's great Serge- it works :). But, now it's got a little more
complicated...

Imagine in the table

contract date
1. CONTRACT A 2005-12-01
2. CONTRACT B 2006-03-01
3. CONTRACT C 2006-06-01
4. CONTRACT A 2006-09-01
5. CONTRACT B 2006-12-01
6. CONTRACT C 2007-03-01
7. CONTRACT A 2007-06-01
8. CONTRACT B 2007-09-01
9. CONTRACT C 2007-12-01

This time, I'm trying to select the first option after the current
date, but for each contract. So if I run the query with current date as
2006-10-09 the return would be

1. CONTRACT B 2006-12-01
2. CONTRACT C 2007-03-01
3. CONTRACT A 2007-06-01

SELECT *
FROM (SELECT *
, ROWNUMBER() OVER(GROUP BY contract ORDER BY date) rn
FROM T
WHERE date ?
) S
WHERE rn = 1
ORDER BY contract;

Oct 9 '06 #4

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

Similar topics

4
by: Sami | last post by:
I hope someone will tell me how to do this without having to do any VB as I know nothing in that area. I am a rank beginner in using Access. I have created a database consisting of student...
1
by: Ramesh | last post by:
hi, I am selecting fields from three table for manupulating data and i want to display total number of records selected. But i am always getting -1 value, eventhough 1000 of records are selected....
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
0
by: | last post by:
I am testing the following piece of code: Is it possible to list the row number containing the checked box? After checking the desired box, I would want a message box to popup listing the row...
5
by: Mahesh S | last post by:
Hi I would like to write a SQL select statement that would fetch rows numbered 50 to 100. Let me clarify, if i say "fetch first 10 rows only", it returns the first 10 rows from the resultset....
4
by: tone | last post by:
Hi, need some help. Got table A with two columns: type DATE, VALUE Got table B with two columns: type DATE, VALUE Now the thing is to update table A with the values from table B. For each...
9
by: Generic Usenet Account | last post by:
I had a need to randomly select an element from an STL collection. It does not appear that this functionality is provided out-of-the-box with STL. Here is my crude implementation. I am using...
4
by: darrel | last post by:
I have a DDL list along these lines: item value="1" text="a" item value="2" text="b" item value="3" text="c" item value="2" text="d" item value="2" text="e" item value="1" text="f" item...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.