pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
order by date desc but NULLs last
5+ messages / 4 participants
[nested] [flat]

* order by date desc but NULLs last
@ 2003-02-09 22:29  A.M. <agentm@cmu.edu>
  0 siblings, 2 replies; 5+ messages in thread

From: A.M. @ 2003-02-09 22:29 UTC (permalink / raw)
  To: pgsql-sql

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.)

Thanks for any advice.
 ><><><><><><><><><
AgentM
agentm@cmu.edu





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: order by date desc but NULLs last
@ 2003-02-09 23:12  Ross J. Reedstrom <reedstrm@rice.edu>
  parent: A.M. <agentm@cmu.edu>
  1 sibling, 1 reply; 5+ messages in thread

From: Ross J. Reedstrom @ 2003-02-09 23:12 UTC (permalink / raw)
  To: A.M. <agentm@cmu.edu>; +Cc: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: order by date desc but NULLs last
@ 2003-02-09 23:12  Tomasz Myrta <jasiek@klaster.net>
  parent: A.M. <agentm@cmu.edu>
  1 sibling, 0 replies; 5+ messages in thread

From: Tomasz Myrta @ 2003-02-09 23:12 UTC (permalink / raw)
  To: A.M. <agentm@cmu.edu>; pgsql-sql

> 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.)
> 
> Thanks for any advice.
>  ><><><><><><><><><
> AgentM
> agentm@cmu.edu

order by gradetime is null asc, gradetime desc;

Regards,
Tomasz Myrta



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: order by date desc but NULLs last
@ 2003-02-10 18:32  Jean-Luc Lachance <jllachan@nsd.ca>
  parent: Ross J. Reedstrom <reedstrm@rice.edu>
  0 siblings, 1 reply; 5+ messages in thread

From: Jean-Luc Lachance @ 2003-02-10 18:32 UTC (permalink / raw)
  To: Ross J. Reedstrom <reedstrm@rice.edu>; +Cc: A.M. <agentm@cmu.edu>; pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: order by date desc but NULLs last
@ 2003-02-10 18:52  Ross J. Reedstrom <reedstrm@rice.edu>
  parent: Jean-Luc Lachance <jllachan@nsd.ca>
  0 siblings, 0 replies; 5+ messages in thread

From: Ross J. Reedstrom @ 2003-02-10 18:52 UTC (permalink / raw)
  To: Jean-Luc Lachance <jllachan@nsd.ca>; +Cc: A.M. <agentm@cmu.edu>; pgsql-sql

On Mon, Feb 10, 2003 at 01:32:54PM -0500, Jean-Luc Lachance wrote:
> Why not try the obvious first?

Because it's not always obvious ;-) Yup, if you want two kinds of order
(NULLs last, descending dates), use two order by clauses. 
 
> order by gradedtime is null, gradedtime desc;
> 
> "Ross J. Reedstrom" wrote:

<a different, less elegant way to get the NULLs last>

Ross




^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2003-02-10 18:52 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-02-09 22:29 order by date desc but NULLs last A.M. <agentm@cmu.edu>
2003-02-09 23:12 ` Ross J. Reedstrom <reedstrm@rice.edu>
2003-02-10 18:32   ` Jean-Luc Lachance <jllachan@nsd.ca>
2003-02-10 18:52     ` Ross J. Reedstrom <reedstrm@rice.edu>
2003-02-09 23:12 ` Tomasz Myrta <jasiek@klaster.net>

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