MySQL Forums
Forum List  »  Newbie

Re: grouping query results in their order
Posted by: Rick James
Date: August 20, 2009 08:29PM

CREATE TEMPORARY TABLE t (
   id INT AUTO_INCREMENT NOT NULL,  # this is the secret sauce
   location VARCHAR(11)  NOT NULL,
   time TIME  NULL,
   PRIMARY KEY (id) );
INSERT INTO t (location, time)
   SELECT location, time FROM tbl
        WHERE person = 'P1'
        ORDER BY time; 
INSERT INTO t ('', NULL);  # Could use NOW() or whatever
Now you have an ordered list of info about the person, including a dummy row for the end. How do a self-join to get the desired pairings.
SELECT a.location,
       a.time AS Start_Time,
       b.time AS End_Time
   FROM t a, t b
   WHERE b.id = a.id+1
   ORDER BY a.time;

I see that this is not what you wanted, since you seem to have both start and end times mixed in the 'time' column, with no indication of which is which. But maybe I have helped you think "out of the box".

Options: ReplyQuote


Subject
Written By
Posted
Re: grouping query results in their order
August 20, 2009 08:29PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.