If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Comparing 2 records

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-16-03, 18:24
jpuehl jpuehl is offline
Registered User
 
Join Date: Dec 2003
Location: Rogue River, Or
Posts: 5
Question Comparing 2 records

I'm trying to write an SQL statement that would allow me to compare 2 records from the same table.

Basically, I have a table of events for an object with a column that contains when the event started but not when the event ended. An object in the table will have at least 2 events so I could take the start time of the second event to determine the end time of the first event. Can this be done using SQL? I know I can do it by writing a stored procedure and storing the results in a table, but was hoping not to have to do this.

Any help will be appreciated.
Reply With Quote
  #2 (permalink)  
Old 12-16-03, 19:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select t1.id
     , t1.starttime as starttime
     , t2.starttime as endtime
  from events t1
inner
  join events t2
    on t1.id = t2.id
   and t1.starttime < t2.starttime
use left outer join instead of inner if you want to pick up events that have started but not finished

rudy
http://r937.com/
Reply With Quote
  #3 (permalink)  
Old 12-17-03, 10:59
jpuehl jpuehl is offline
Registered User
 
Join Date: Dec 2003
Location: Rogue River, Or
Posts: 5
Thanks Rudy!
Reply With Quote
  #4 (permalink)  
Old 12-17-03, 11:26
jpuehl jpuehl is offline
Registered User
 
Join Date: Dec 2003
Location: Rogue River, Or
Posts: 5
Actually, the version of Oracle I am using did not support "inner join", so what ended up working was:

select t1.id, t1.date, t2.date
from event t1, event t2
where t1.id = t2.id
and t1.date < t2.date
Reply With Quote
  #5 (permalink)  
Old 12-17-03, 11:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
good one, and thanks for the update

i keep forgetting that there are still lots of companies and individuals out there who are in no hurry to install the latest and greatest software

e.g. i'm still on access 97 on my desktop, and have no plans to upgrade ever

by the way, what side does the plus sign go on if you wanted to join event t1 left outer event t2?

i could look it up, i guess...
Reply With Quote
  #6 (permalink)  
Old 12-18-03, 07:17
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
The (+) goes on the "outer" side of the join, i.e.

t1.id = t2.id (+)
and t1.starttime < t2.starttime (+)
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On