ORDER BY TIMESTAMP_column ASC, NULL first - Mailing list pgsql-sql

From Fredrik Wendt
Subject ORDER BY TIMESTAMP_column ASC, NULL first
Date
Msg-id 20040212100626.GA21864@csbnet.se
Whole thread Raw
Responses Re: ORDER BY TIMESTAMP_column ASC, NULL first  (Tomasz Myrta <jasiek@klaster.net>)
Re: ORDER BY TIMESTAMP_column ASC, NULL first  (Stephan Szabo <sszabo@megazone.bigpanda.com>)
Re: ORDER BY TIMESTAMP_column ASC, NULL first  (Rod Taylor <pg@rbt.ca>)
Re: ORDER BY TIMESTAMP_column ASC, NULL first  (Bruno Wolff III <bruno@wolff.to>)
List pgsql-sql
Hi!

I read posts telling me that NULL values are considered greater than
non-null values. Fine. Is there a way to explicitly reverse this?

I have the situation where a table holds IP-addresses. The table has
column of type timestamp, called assignedAt, which tells when the
address was assigned to a computer in our network.
What I'd like to do, is to get an IP-address that hasn't yet been
assigned to anyone, or the one with the smallest assignedAt (most likely
to not be in use).

CREATE TABLE IPv4Address (id        SERIAL PRIMARY KEY,address        INET NOT NULL UNIQUE,assignedAt    TIMESTAMP
);

The query to use would be,
SELECT ip.id FROM IPv4Address ip ORDER BY assignedAt ASC LIMIT 1;

with the exception that this returns rows with NULL at the end, instead
of at the beginning which is what I'd like.

How do achieve this with one query? I'm using Postgres 7.4.

And oh, I'm not on the list so please cc my adress in any replies!


Thanks in advance,
Fredrik Wendt


pgsql-sql by date:

Previous
From: "Ivo Anon"
Date:
Subject: nextval problem
Next
From: "Sumita Biswas (sbiswas)"
Date:
Subject: Function