X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id E1DF1D1C4F1 for ; Wed, 23 Jul 2003 20:49:57 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 61708-04 for ; Wed, 23 Jul 2003 17:49:16 -0300 (ADT) Received: from tomts9-srv.bellnexxia.net (tomts9.bellnexxia.net [209.226.175.53]) by svr1.postgresql.org (Postfix) with ESMTP id D01C0D1C4DF for ; Wed, 23 Jul 2003 17:49:55 -0300 (ADT) Received: from acer.sympatico.ca ([67.69.230.6]) by tomts9-srv.bellnexxia.net (InterMail vM.5.01.05.32 201-253-122-126-132-20030307) with ESMTP id <20030723204957.QHBC3155.tomts9-srv.bellnexxia.net@acer.sympatico.ca> for ; Wed, 23 Jul 2003 16:49:57 -0400 Message-Id: <5.2.1.1.0.20030723152501.00a0cd90@pop6.sympatico.ca> X-Sender: b1wudu74@pop6.sympatico.ca X-Mailer: QUALCOMM Windows Eudora Version 5.2.1 Date: Wed, 23 Jul 2003 16:45:24 -0400 To: pgsql-sql@postgresql.org From: Frank Bax Subject: strange "order by" request Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200307/350 X-Sequence-Number: 14464 Two tables - employee and timesheet simple enough. Each table is more complex than example before. timesheet contains multiple rows per day because the "other" fields are different for various rows on same day/employee. My selection criteria actually uses some of these other fields. lo_shift and hi_shift are always on the same date for a given row. In the output, I *must* have all rows for an employee together, the sequence of employees should be based on lowest value of "lo_shift" for each employee. How do I code such a select? Here are some commands to create tables & sample data: CREATE TABLE "employee" ( "emp" character(6) NOT NULL, "last" character varying(64), "first" character varying(64) ); CREATE TABLE "timesheet" ( "emp" character(6) NOT NULL, "dept" character(2) NOT NULL, "lo_shift" timestamp, "hi_shift" timestamp ); insert into employee(emp,last,first) values('091006','Clarke','Cynthia'); insert into employee(emp,last,first) values('096005','Mullins','Denise'); insert into employee(emp,last,first) values('089068','Johnson','Michelle'); insert into employee(emp,last,first) values('098036','Zandstra','Nicole'); insert into timesheet(emp,dept,lo_shift,hi_shift) values('091006','10','2003-07-17 00:00','2003-07-17 07:59'); insert into timesheet(emp,dept,lo_shift,hi_shift) values('091006','10','2003-07-17 08:00','2003-07-17 11:59'); insert into timesheet(emp,dept,lo_shift,hi_shift) values('096005','10','2003-07-17 07:30','2003-07-17 08:59'); insert into timesheet(emp,dept,lo_shift,hi_shift) values('096005','10','2003-07-17 09:00','2003-07-17 17:59'); insert into timesheet(emp,dept,lo_shift,hi_shift) values('098036','10','2003-07-17 13:30','2003-07-17 19:29'); insert into timesheet(emp,dept,lo_shift,hi_shift) values('098036','10','2003-07-17 19:30','2003-07-17 21:29'); insert into timesheet(emp,dept,lo_shift,hi_shift) values('089068','10','2003-07-17 14:00','2003-07-17 17:59'); insert into timesheet(emp,dept,lo_shift,hi_shift) values('089068','10','2003-07-17 18:00','2003-07-17 21:59'); insert into timesheet(emp,dept,lo_shift,hi_shift) values('000032','90','2003-07-18 18:00','2003-07-17 23:59'); SELECT emp.emp, emp.last, emp.first, ts.lo_shift, ts.hi_shift FROM timesheet ts, employee emp WHERE ts.emp = emp.emp AND ts.dept='10' AND ts.lo_shift::date = '2003-07-17' ORDER BY emp.first, emp.last, emp.emp, ts.lo_shift, ts.hi_shift;