Sorting by NULL values

Lists: pgsql-sql
From: Josh Berkus <josh(at)agliodbs(dot)com>
To: Ian Burrell <ib(at)onsitetech(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Sorting by NULL values
Date: 2003-03-04 19:01:52
Message-ID: 200303041101.52565.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Ian,

> I am doing a query where I need to sort by a column that may be NULL
> because it is coming from an OUTER JOIN. I noticed a difference between
> PostgreSQL and other databases about where NULLs show up. It seems that
> with Postgres, NULLs are sorted after other values. Other databases
> sort them before.

Not all other databases. It really depends.

> Is there a way to replace the NULLs with
> empty strings?

Sure: UPDATE table SET column = '' WHERE column IS NULL

--
Josh Berkus
josh(at)agliodbs(dot)com
Aglio Database Solutions
San Francisco


From: Ian Burrell <ib(at)onsitetech(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Sorting by NULL values
Date: 2003-03-04 19:45:02
Message-ID: 3E65023E.10404@onsitetech.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

I am doing a query where I need to sort by a column that may be NULL
because it is coming from an OUTER JOIN. I noticed a difference between
PostgreSQL and other databases about where NULLs show up. It seems that
with Postgres, NULLs are sorted after other values. Other databases
sort them before.

Is there any standard on how sorting NULLs work? Is there a way to
change Postgres's behavior? Is there a way to replace the NULLs with
empty strings?

This is my query:

SELECT product_id
FROM product p
LEFT OUTER JOIN attribute_data ad
ON p.product_id= ad.product_id
AND ad.attribute_id = ?
WHERE p.category_id = ?
ORDER BY data;

The tables look like:

CREATE TABLE product (
product_id serial NOT NULL PRIMARY KEY,
category_id integer NOT NULL
);

CREATE TABLE attribute_data (
attribute_id integer NOT NULL,
product_id integer NOT NULL,
data varchar(1024),
PRIMARY KEY (attribute_id, product_id)
);

- Ian


From: Tomasz Myrta <jasiek(at)klaster(dot)net>
To: Ian Burrell <ib(at)onsitetech(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Sorting by NULL values
Date: 2003-03-04 20:01:31
Message-ID: 3E65061B.9020907@klaster.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Ian Burrell wrote:
> I am doing a query where I need to sort by a column that may be NULL
> because it is coming from an OUTER JOIN. I noticed a difference between
> PostgreSQL and other databases about where NULLs show up. It seems that
> with Postgres, NULLs are sorted after other values. Other databases
> sort them before.
>
> Is there any standard on how sorting NULLs work? Is there a way to
> change Postgres's behavior? Is there a way to replace the NULLs with
> empty strings?
order by field is null asc/desc, field asc/desc

Regards,
Tomasz Myrta


From: Dan Langille <dan(at)langille(dot)org>
To: Ian Burrell <ib(at)onsitetech(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Sorting by NULL values
Date: 2003-03-04 20:07:29
Message-ID: 20030304150627.H38754@xeon.unixathome.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Tue, 4 Mar 2003, Ian Burrell wrote:

> Is there any standard on how sorting NULLs work? Is there a way to
> change Postgres's behavior? Is there a way to replace the NULLs with
> empty strings?

I think what you want may be either COALESCE or ISNULL/IFNULL or perhaps a
CASE statement.


From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Ian Burrell <ib(at)onsitetech(dot)com>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Sorting by NULL values
Date: 2003-03-04 20:13:05
Message-ID: 20030304120713.W55865-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Tue, 4 Mar 2003, Ian Burrell wrote:

> I am doing a query where I need to sort by a column that may be NULL
> because it is coming from an OUTER JOIN. I noticed a difference between
> PostgreSQL and other databases about where NULLs show up. It seems that
> with Postgres, NULLs are sorted after other values. Other databases
> sort them before.

> Is there any standard on how sorting NULLs work? Is there a way to

IIRC, they're either considered greater than or less than non-NULL values,
but the decision is up to the implementation.

> change Postgres's behavior? Is there a way to replace the NULLs with
> empty strings?

Coalesce should work.


From: Rod Taylor <rbt(at)rbt(dot)ca>
To: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
Cc: Ian Burrell <ib(at)onsitetech(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Sorting by NULL values
Date: 2003-03-04 22:01:59
Message-ID: 1046815319.15229.87.camel@jester
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Tue, 2003-03-04 at 15:13, Stephan Szabo wrote:
> On Tue, 4 Mar 2003, Ian Burrell wrote:
>
> > I am doing a query where I need to sort by a column that may be NULL
> > because it is coming from an OUTER JOIN. I noticed a difference between
> > PostgreSQL and other databases about where NULLs show up. It seems that
> > with Postgres, NULLs are sorted after other values. Other databases
> > sort them before.
>
> > Is there any standard on how sorting NULLs work? Is there a way to

If you care, order by their boolean equivelent first:

order by field is null desc, field

DESC puts nulls first, since true > false

> IIRC, they're either considered greater than or less than non-NULL values,
> but the decision is up to the implementation.
>
> > change Postgres's behavior? Is there a way to replace the NULLs with
> > empty strings?
>
> Coalesce should work.
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)
--
Rod Taylor <rbt(at)rbt(dot)ca>

PGP Key: http://www.rbt.ca/rbtpub.asc