X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 7B101474E5C for ; Sun, 9 Feb 2003 18:12:05 -0500 (EST) Received: from ece.rice.edu (ece.rice.edu [128.42.4.34]) by postgresql.org (Postfix) with ESMTP id 12DA4474E53 for ; Sun, 9 Feb 2003 18:12:05 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by ece.rice.edu (Postfix) with SMTP id 8F34A68A83; Sun, 9 Feb 2003 17:12:07 -0600 (CST) Received: from localhost (localhost [127.0.0.1]) by ece.rice.edu (Postfix) with ESMTP id 3199B68A7B; Sun, 9 Feb 2003 17:12:07 -0600 (CST) Received: from wallace.ece.rice.edu (wallace.ece.rice.edu [128.42.12.154]) by ece.rice.edu (Postfix) with ESMTP id 2A28D68A5B; Sun, 9 Feb 2003 17:12:05 -0600 (CST) Received: from reedstrm by wallace.ece.rice.edu with local (Exim 3.36 #1 (Debian)) id 18i0cG-00037u-00; Sun, 09 Feb 2003 17:12:04 -0600 Date: Sun, 9 Feb 2003 17:12:04 -0600 From: "Ross J. Reedstrom" To: "A.M." Cc: pgsql-sql@postgresql.org Subject: Re: order by date desc but NULLs last Message-ID: <20030209231204.GB11550@wallace.ece.rice.edu> Mail-Followup-To: "Ross J. Reedstrom" , "A.M." , pgsql-sql@postgresql.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4i X-Virus-Scanned: by AMaViS snapshot-20020300 X-DCC--Metrics: ece.rice.edu 1066; Body=1 Fuz1=1 Fuz2=1 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/181 X-Sequence-Number: 11579 On Sun, Feb 09, 2003 at 05:29:29PM -0500, A.M. wrote: > I have a simple query that sorts by descending date but the NULL dates > show up first. Is there a way I can sort so they come last without > sorting ascending? > > SELECT submittime,score,gradedtime FROM student_gradedmaterial WHERE > gradedmaterialid=3 and studentid=102 order by gradedtime desc; > > submittime | score | gradedtime > ---------------------+-------+---------------------------- > 2003-01-30 22:56:38 | | > 2003-01-31 03:42:29 | 99 | 2003-02-06 14:21:43.043587 > > but what I want is all the graded items first in gradedtime desc and > NULL afterwards. I do need to keep the NULL score rows. (So I get the > latest submitted grade for the assignment but also any ungraded > submission information.) You need to ORDER BY a _function_ of the gradedtime column, substituting an extreme value for NULL. Try this: SELECT submittime,score,gradedtime FROM student_gradedmaterial WHERE gradedmaterialid=3 and studentid=102 order by coalesce(gradedtime,'-infinity') desc; Ross