Re: Numbering rows by date

From: brian <brian(at)zijn-digital(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Numbering rows by date
Date: 2008-04-05 23:37:02
Message-ID: 47F80D1E.1080201@zijn-digital.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andrus wrote:
> I have table
>
> create Document ( docdate date, docorder integer )
>
> I need update docorder column with numbers 1,2 in docdate date order
> Something like
>
> i = 1;
> UPDATE Document SET docorder = i++
> ORDER BY docdate;
>
>
> How to do this is PostgreSQL 8.2 ?
>

ALTER TABLE DROP COLUMN docorder;
SELECT docdate FROM document ORDER BY docdate ASC;

Or, if you really need the numbering and can't do it in application code
... my first thought was to do this:

CREATE TEMP SEQUENCE seq_doc_number;
SELECT docdate, nextval('seq_doc_number') FROM document
ORDER BY docdate ASC;

But the ordering will occur afterwards so the sequence will be out of
order. I think you'd need a subquery, then:

CREATE TEMP SEQUENCE seq_doc_number;
SELECT docdate, nextval('seq_doc_number') AS docorder
FROM (SELECT docdate FROM document ORDER BY docdate ASC) dummy_alias;

b

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Dee 2008-04-06 01:03:52 Re: Cannot Install PostgreSQL on Windows 2000 Server
Previous Message Joey K. 2008-04-05 23:10:49 Limiting postgresql resources