From: Ross J. Reedstrom <reedstrm@rice.edu>
To: A.M. <agentm@cmu.edu>
Cc: pgsql-sql@postgresql.org
Subject: Re: order by date desc but NULLs last
Date: Sun, 9 Feb 2003 17:12:04 -0600
Message-ID: <20030209231204.GB11550@wallace.ece.rice.edu> (raw)
In-Reply-To: <F427C52A-3C7D-11D7-A094-0030657192DA@cmu.edu>
References: <F427C52A-3C7D-11D7-A094-0030657192DA@cmu.edu>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: reedstrm@rice.edu, agentm@cmu.edu
Subject: Re: order by date desc but NULLs last
In-Reply-To: <20030209231204.GB11550@wallace.ece.rice.edu>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox