Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
An SQL-query question[SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
larand54
l33t
l33t


Joined: 20 Feb 2004
Posts: 695
Location: Sweden

PostPosted: Sat Sep 29, 2007 5:36 pm    Post subject: An SQL-query question[SOLVED] Reply with quote

I got a table tha are keyed with dates.
This table is populated with a set of records entered at different dates during a few years.

I want to enter a date that can be between two records and get the result from the nearest previous date and the nearest succeding date.

I thought I could solve this using two SQL-querys but I failed.

An example of the table:
Code:

Date           Value
2004-12-16      1000
2005-05-30     10000
2005-08-30     18560
2005-11-05     29930
....
....


Suppose I got the the date 2005-06-22 and want an interpolated value for that date. Then I need to get the dates 2005-05-30 and 2005-08-30.

How can I solve this with SQL ?


Last edited by larand54 on Sat Sep 29, 2007 7:42 pm; edited 1 time in total
Back to top
View user's profile Send private message
nixnut
Bodhisattva
Bodhisattva


Joined: 09 Apr 2004
Posts: 10974
Location: the dutch mountains

PostPosted: Sat Sep 29, 2007 5:45 pm    Post subject: Reply with quote

Something like this ought to do it I think:
Code:
select max(Value) AS LowerValue from Mytable where Date < 2005-06-22
union
select min (Value) AS UpperValue from Mytable where Date > 2005-06-22

_________________
Please add [solved] to the initial post's subject line if you feel your problem is resolved. Help answer the unanswered

talk is cheap. supply exceeds demand
Back to top
View user's profile Send private message
larand54
l33t
l33t


Joined: 20 Feb 2004
Posts: 695
Location: Sweden

PostPosted: Sat Sep 29, 2007 6:45 pm    Post subject: Reply with quote

Hmm....It works but I also need the date for each value but that's not possible to include in the select as I see it...or is it possible?
Back to top
View user's profile Send private message
nixnut
Bodhisattva
Bodhisattva


Joined: 09 Apr 2004
Posts: 10974
Location: the dutch mountains

PostPosted: Sat Sep 29, 2007 7:08 pm    Post subject: Reply with quote

This maybe?
Code:
select max(Date) AS LowerDate, max(Value) AS LowerValue from Mytable where Date < 2005-06-22
union
select min(Date) AS UpperDate, min (Value) AS UpperValue from Mytable where Date > 2005-06-22

Mind you, you don't want to run this on very large datasets.
_________________
Please add [solved] to the initial post's subject line if you feel your problem is resolved. Help answer the unanswered

talk is cheap. supply exceeds demand
Back to top
View user's profile Send private message
larand54
l33t
l33t


Joined: 20 Feb 2004
Posts: 695
Location: Sweden

PostPosted: Sat Sep 29, 2007 7:42 pm    Post subject: Reply with quote

Perfect! :D
I have a lot to learn...Thanks!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum