Re: Group By, Aggregate Functions and NULL

From: "Andrew Hammond" <andrew(dot)george(dot)hammond(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Group By, Aggregate Functions and NULL
Date: 2006-07-27 18:12:03
Message-ID: 1154023923.713545.144900@i42g2000cwa.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Brendon Gleeson wrote:
> I need to Group By a date column that may contain a NULL value. Min() only
> gives me the lowest date and not NULL if it exists. How do I get a Group By
> clause to give me NULL if it exists?

There are a couple of ways to approach this, but it depends on what
exactly you're trying to accomplish, which is not completely clear from
your question. I'll take a shot in the dark and guess that you want all
the NULLs to group together and sort out lower than your other values.
If that's what you want then perhaps a query such as the following
would work for you:

SELECT COALESCE(ts, '-infinity'::timestamp) FROM mytable GROUP BY 1
ORDER BY 1;

For further details please see the following:
http://www.postgresql.org/docs/current/static/functions-conditional.html
http://www.postgresql.org/docs/current/static/datatype-datetime.html

Drew

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Andrew Hammond 2006-07-27 18:52:03 Re: PostgreSQL Replication
Previous Message Brendon Gleeson 2006-07-27 17:25:10 Group By, Aggregate Functions and NULL