Skip site navigation (1) Skip section navigation (2)

Re: Call for 7.5 feature completion

From: Dennis Bjorklund <db(at)zigo(dot)dhs(dot)org>
To: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
Cc: David Fetter <david(at)fetter(dot)org>,Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>,<pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Call for 7.5 feature completion
Date: 2005-08-29 12:14:01
Message-ID: Pine.LNX.4.44.0508291341340.2995-100000@zigo.dhs.org (view raw or flat)
Thread:
Lists: pgsql-hackers
On Mon, 29 Aug 2005, Christopher Kings-Lynne wrote:

> Oh, and 'select rowid, * from table' which returns special rowid column 
> that just incrementally numbers each row.

In sql2003 there is a window function called ROW_NUMBER() that can be used
to get numbers like that (one also need to specify the window to be the
full table in this case).

I think it can look like this (based on me reading the standard, i've not 
tested it in one of the other databases that support window functions):

SELECT ROW_NUMBER() OVER (ORDER BY id), * FROM table;

The over part specify that the whole result set is the window and that the 
row numbers should be assigned to the result in that order. In practice 
you want that order to be the same as the whole order I guess

SELECT ROW_NUMBER() OVER (ORDER BY id), * FROM table ORDER BY id;

Based on some googeling DB2 seems to allow OVER () while oracle does not
and you need to specify the ORDER BY (or some other window definition) in 
the OVER part.

Anyway, I just want to point out that row numbers are possible to get in
sql2003, even if a simpler syntax like the above can also be useful. Maybe
one can just extend sql2003 and let the OVER part be optional all
together, and use SELECT ROW_NUMBER(), * FROM table;


ps.

A window is similar to group by, but you keep all rows in the result set.  
With group by you get one row from each group in the result set,

-- 
/Dennis Björklund


In response to

Responses

pgsql-hackers by date

Next:From: Ilia KantorDate: 2005-08-29 13:04:33
Subject: Selectivity function argument: Const -> Array
Previous:From: Robert TreatDate: 2005-08-29 12:12:37
Subject: Re: [HACKERS] Improved \df(+) in psql + backward-compatibility

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group