Skip to main content
gamedev.net
🔒 Locked

SQL Query Question

Started by d000hg 0 replies 950+ views
Original Post
d000hg
d000hg
This isn't game-related but something I'm stuck on at work, I need a quick answer and GD is the biggest coding community I know... The problem 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:
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 (the DB we use) has 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. I posted also at DBForums.com - if anyone can recommend a great DB-related community that would be cool for future reference.
Sephirox
Sephirox
try something like:
select a.date, max(b.date) from A a left join B b on b.date < a.date

thats just te top of my head at 6am (first day to change my sched for school)...

i'll make sure it works once i get to work, at around 9.

*edit*
I'm stumped. Guess I don't know enough about SQL, Sorry.
Look into for loops with sql - I didn't cause I don't know if they're standard or not w/e (I learned SQL from w3schools.com, they don't mention for loops...lol)

psuedo code:
for each record in A do
select max(B) where B < A

[Edited by - Sephirox on August 8, 2007 8:43:43 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.