Friday, October 31. 2008Simulating Row Number in PostgreSQL Pre 8.4Printer FriendlyComments
Display comments as
(Linear | Threaded)
Couldn't the last example be refined using row wise comparison instead of field concatenation?
Richard,
You mean something like ROW(t.last_name, t.first_name) ... Hadn't thought about that. I guess that would work.
Yes :)
Here it is, a bit cleaner and a bit more flexible, as we can put in the ROW(...) comparison the fields in the order by which we want the output to be ordered: SELECT (SELECT COUNT(*) FROM people AS P1 WHERE ( ROW(P1.last_name,P1.first_name,P1.id) <= ROW(P2.last_name,P2.first_name,P2.id) ) ) AS row_number, last_name,first_name FROM people AS P2 ORDER BY row_number ; As said by Regina, a field UNIQUE (id in this case) is required.
can #1 or #3 be adapted to group on an additional field.
so in the example that you have shown you would get # name category 1 wgates A 2 wgrant A 1 jjones B 2 psmith B That would be AWESOME!
Hi--I tried to link to this using the trackback link ( http://www.postgresonline.com/journal/comment.php?type=trackback&entry_id=79 ), but I get the error message " 1 Danger Will Robinson, trackback failed.". Any ideas?
Strange I wonder if its just because our track back system doesn't allow track backs after a certain time. Though it should have come in as moderated.
Anyrate we'll put your entry in as a trackback.
Hi, take a look at this code. If this set of functions exist, you can run queries like this
select counter(get_counter() * -1),* from and get the rows always numbered 1...n CREATE OR REPLACE FUNCTION counter(int) RETURNS INT AS $$ return $_[0] + $_SHARED{counter}++; $$ LANGUAGE plperl; CREATE OR REPLACE FUNCTION get_counter() RETURNS INT AS $$ return $_SHARED{counter}++; $$ immutable LANGUAGE plperl;
This is just what I need to get started ... thanks. But what I really need to do is to delete a row WHERE row_line == x. How would I do that?
Dan,
Well we wrote a different article for delete, that is probably more appropriate for you. http://www.postgresonline.com/journal/index.php?/archives/22-Deleting-Duplicate-Records-in-a-Table.html You might also want to lookup at PostgreSQL row() function The row(..) operator may do the trick for you though will probably be slow. DELETE FROM people WHERE row(people.*) = row('jjones', 'Jones', 'John') ;
select name_id, last_name, first_name, generate_series(1,9999)
from table_name
--//create temporary sequence myrow;
ALTER SEQUENCE myrow restart 1; select nextval('myrow'),stock.* from stock;
Too using a secuence:
select nextval('temp_seq')-1 as top, twotable.consumo_total, twotable.valor_comprado, twotable.valor_producido, twotable.valor_trueque, twotable.valor_otras, twotable.alimento FROM ( select * from (SELECT 10 as id, public.foodmore.consumo_total, public.foodmore.valor_comprado, public.foodmore.valor_producido, public.foodmore.valor_trueque, public.foodmore.valor_otras, public.foodmore.alimento FROM public.foodmore union select setval('temp_seq',1)-2 As top, 1,2,3,4,5,'6') as onetable where id=10 order by consumo_total desc ) as twotable |
Entry's LinksQuicksearchCalendar
Categories
Blog Administration |
I recently ran across this post on simulating row numbering in postgres (and announcing that 8.4 will finally have a function for this). I was aware of the temporary sequence solution (I believe that’s the stock answer to that question in postgres) , but the two other approaches are new to me (and rather clever). The last especially is… majestic:
Tracked: Feb 19, 13:02