X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id BEF04475A3F for ; Mon, 10 Feb 2003 13:33:20 -0500 (EST) Received: from beamish.nsd.ca (beamish.nsd.ca [205.150.156.194]) by postgresql.org (Postfix) with ESMTP id 044F34759BD for ; Mon, 10 Feb 2003 13:33:20 -0500 (EST) Received: (from smap@localhost) by beamish.nsd.ca (8.9.3/8.9.3) id NAA10377; Mon, 10 Feb 2003 13:33:17 -0500 X-Authentication-Warning: beamish.nsd.ca: smap set sender to using -f Received: from reddog.nsd.ca(192.168.101.30) by beamish.nsd.ca via smap (V2.1/2.1+anti-relay+anti-spam) id xma010373; Mon, 10 Feb 03 13:32:56 -0500 Received: from nsd.ca (jllachan-linux.nsd.ca [192.168.101.148]) by reddog.nsd.ca (8.8.7/8.8.7) with ESMTP id NAA25767; Mon, 10 Feb 2003 13:29:46 -0500 Message-ID: <3E47F056.DE8707E2@nsd.ca> Date: Mon, 10 Feb 2003 13:32:54 -0500 From: Jean-Luc Lachance X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.4.9-31 i686) X-Accept-Language: en MIME-Version: 1.0 To: "Ross J. Reedstrom" Cc: "A.M." , pgsql-sql@postgresql.org Subject: Re: order by date desc but NULLs last References: <20030209231204.GB11550@wallace.ece.rice.edu> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/198 X-Sequence-Number: 11596 Why not try the obvious first? order by gradedtime is null, gradedtime desc; "Ross J. Reedstrom" wrote: > > 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 > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly