Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I am very happy with the whole site and would like to extend my compliments to all of you who work to make it one of the most useful sites (If not THE Most Useful) ...and the easiest to navigate..."

Geography

Where in the world do Tek-Tips members come from?
d000hg (Programmer)
8 Aug 07 7:19
This is pretty simple to describe:
Table A & B both have a date field. I need a query/procedure which will do:
SELECT A.date, { max(B.date) where B.date < A.date}
Example:

CODE

A.date.... B.date
======.... ======
10/10/2006 01/01/1999
01/04/2007 12/12/2006

Result: A.date | Max B.date < A.date
======..===================
10/10/2006 01/01/1999 // the biggest date in B which is < than 10/10/2006
01/04/2007 12/12/2006
So each row contains the date of an A, and the biggest date in B which is less than this value. It seems like it should be so simple but I just can't get it to work in a query, and I don't know the syntax Sybase use for writing procedures - if I were in Oracle I'd have given up and written a FOR loop by now... but a query would be neater anyway.

Many thanks for any help.
Hans63 (Programmer)
9 Aug 07 2:44
Hi,

your question lacks a little information. Do you have any other join conditions between tables A and B?
What RDBMS are you using (Oracle, DB2, SQLserver, etc)?

But let's give it a try (this is based on Oracle):
select a.date
     , max(case
           when a.date > b.date then b.date
           else null
           end)
from table1 a
   , table2 b
where join_condition
group by a.date

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close