Re: Numbering rows by date - Mailing list pgsql-general

From brian
Subject Re: Numbering rows by date
Date
Msg-id 47F80D1E.1080201@zijn-digital.com
Whole thread Raw
In response to Numbering rows by date  ("Andrus" <kobruleht2@hot.ee>)
List 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


pgsql-general by date:

Previous
From: "Joey K."
Date:
Subject: Limiting postgresql resources
Next
From: Dee
Date:
Subject: Re: Cannot Install PostgreSQL on Windows 2000 Server