Обсуждение: Group By, Aggregate Functions and NULL

Поиск
Список
Период
Сортировка

Group By, Aggregate Functions and NULL

От
Brendon Gleeson
Дата:
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?

Re: Group By, Aggregate Functions and NULL

От
"Andrew Hammond"
Дата:
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