Search Postgresql Archives

Re: Numbering rows by date

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux