X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 486CC9F9F2D for ; Sun, 18 Jun 2006 07:17:16 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 62515-10 for ; Sun, 18 Jun 2006 07:17:09 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id 80AC89F9AD3 for ; Sun, 18 Jun 2006 07:17:09 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 0789030939; Sun, 18 Jun 2006 12:17:08 +0200 (MET DST) From: Sim Zacks X-Newsgroups: pgsql.sql Subject: join on next row Date: Sun, 18 Jun 2006 13:14:33 +0200 Organization: Hub.Org Networking Services Lines: 44 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Thunderbird 1.5.0.4 (Windows/20060516) To: pgsql-sql@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/243 X-Sequence-Number: 25479 I am having brain freeze right now and was hoping someone could help me with a (fairly) simple query. I need to join on the next row in a similar table with specific criteria. I have a table with events per employee. I need to have a query that gives per employee each event and the event after it if it happened on the same day. The Events table structure is: EventID Employee EventDate EventTime EventType I want my query resultset to be Employee,EventDate(1),EventTime(1),EventType(1),EventTime(2),EventType(2) Where Event(2) is the first event of the employee that took place after the other event. Example EventID Employee EventDate EventTime EventType 1 John 6/15/2006 7:00 A 2 Frank 6/15/2006 7:15 B 3 Frank 6/15/2006 7:17 C 4 John 6/15/2006 7:20 C 5 Frank 6/15/2006 7:25 D 6 John 6/16/2006 7:00 A 7 John 6/16/2006 8:30 R Expected Results John, 6/15/2006, 7:00, A, 7:20, C Frank, 6/15/2006, 7:15, B, 7:17, C Frank, 6/15/2006, 7:17, C, 7:25, D John, 6/16/2006, 7:00, A, 8:30, R To get this result set it would have to be an inner join on employee and date where the second event time is greater then the first. But I don't want the all of the records with a greater time, just the first event after. Thank You Sim