User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 18 Apr 2005, 13:46   #1
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
SQL - Selecting a value from the record before the current one (tough one)

Another SQL question from me, this one I doubt can even be done (though they say you can do everything with SQL no?).
I want to compose a record from which one field contains a value from the record inserted before. I need this to calculate a time-difference between two records.

The table looks as follows:
1 - 9:00
2 - 9:30
3 - 10:30

I want a result like:
1 - 9:00 - 0:00
2 - 9:30 - 9:00
3 - 10:30 - 9:30

I tried a few combinations and came as far as:
SELECT Time, (SELECT MAX(Time) FROM Time_RawData WHERE Time < Time AND Date = Time_RawData.Date) AS secs
FROM Time_RawData;

But no success so far.
I tried using aliases like:
SELECT Time as thistime, (SELECT MAX(Time) FROM Time_RawData WHERE Time < thistime AND Date = Time_RawData.Date) AS secs
FROM Time_RawData;
But that doesn't work either. I'm using Access as a test environment and it asks me for the value of "thistime" so it can not figure this backreference out.

So, how do I get the previous time in the current record?
__________________
"Yay"
Structural Integrity is offline   Reply With Quote
Unread 18 Apr 2005, 17:34   #2
wu_trax
Registered User
 
Join Date: Jan 2003
Posts: 4,290
wu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet society
Re: SQL - Selecting a value from the record before the current one (tough one)

can you add another field that refers to the row before? then you could join table with itself.
something like
select a.time, (b.time - a.time) as secs from time_raw_data a, time_raw_data b where a.last_id = b.id or something like that.
there probably is a better way, i just dont know enough about sql.
(i guess you probably want to avoid ids, so this is no help :/)
__________________
im not tolerant, i just dont care.
wu_trax is offline   Reply With Quote
Unread 18 Apr 2005, 18:38   #3
Caesar2
Commander
 
Caesar2's Avatar
 
Join Date: Sep 2001
Location: Netherlands
Posts: 146
Caesar2 is just really niceCaesar2 is just really niceCaesar2 is just really niceCaesar2 is just really nice
Re: SQL - Selecting a value from the record before the current one (tough one)

Code:
SELECT   a.Time AS End,
         iif((SELECT   TOP 1 Time
              FROM     Time_RawData
              WHERE    a.Time >Time_RawData.Time
              ORDER BY Time DESC) IS NULL,
              "0:00",
              (SELECT   TOP 1 Time
               FROM     Time_RawData
               WHERE    a.Time >Time_RawData.Time
               ORDER BY Time DESC)) AS Begin
FROM     Time_RawData AS a
ORDER BY a.Time;
Link the table in your FROM with the table in the query that's in your SELECT statement.
SELECT the highest value that is smaller than your End Time
REPLACE a NULL value with 0:00 (or something else).
Hope this what you wanted.
__________________
Quote:
Originally posted by Cochese
Cathaar are not overpowered.

You were just "bashed", live with it.
Caesar2 is offline   Reply With Quote
Unread 18 Apr 2005, 18:39   #4
Dante Hicks
Clerk
 
Join Date: Jun 2001
Posts: 13,940
Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Dante Hicks has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: SQL - Selecting a value from the record before the current one (tough one)

Usually when using subqueries it's easier to reference the table as well as the column name.

So for your bit of code :
SELECT
Time as thistime
(SELECT MAX("Time") FROM Time_RawData WHERE "Time" < Time2."ThisTime")
FROM Time_RawDate as Time2

That way the subquery knows which column you're referring to.

However, I have no idea whether this will work in Access. I think it would work in Sybase. A self-join should also work though as per wu_traxs suggestion.

Also, you really should refrain from calling columns things like "Date" or "Time", unless I'm misunderstanding what you're doing.
Dante Hicks is offline   Reply With Quote
Unread 18 Apr 2005, 23:39   #5
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Re: SQL - Selecting a value from the record before the current one (tough one)

You don't even need subqueries:
select a.t, ifnull(max(b.t),0) from data a left join data b on b.t<a.t group by a.t
This isn't a tough one.
queball is offline   Reply With Quote
Unread 21 Apr 2005, 07:20   #6
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Re: SQL - Selecting a value from the record before the current one (tough one)

OK, thanks everyone. Creating an alias for the table was the solution. I didn't know that was possible.
The following query will grace this app in the future:

SELECT empl, date, Time AS thistime, (SELECT MAX(Time) FROM Time_RawData WHERE Time < t.Time AND Date = t.Date AND t.Empl = Empl) AS prevtime
FROM Time_RawData AS t
ORDER BY date, empl, time;

edit:
Found out I also need a reference to the next record:
SELECT TRD_RunNr, empl, date, Time AS thistime, (SELECT MAX(Time) FROM Time_RawData WHERE Time < t.Time AND Date = t.Date AND t.Empl = Empl) AS prevtime, (SELECT TRD_RunNr FROM Time_RawData WHERE Time = (SELECT MIN(Time) FROM Time_RawData WHERE Time > t.Time AND Date = t.Date AND t.Empl = Empl) AND Date = t.Date AND t.Empl = Empl) AS nextrecord
FROM Time_RawData AS t
ORDER BY date, empl, time;

This subquery stuff rocks!
__________________
"Yay"

Last edited by Structural Integrity; 21 Apr 2005 at 07:47.
Structural Integrity is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 00:41.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018