X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 0EA6B9FA279 for ; Fri, 26 May 2006 11:06:30 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 48584-04 for ; Fri, 26 May 2006 11:06:19 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from wolff.to (wolff.to [66.93.197.194]) by postgresql.org (Postfix) with SMTP id B94E89F9B01 for ; Fri, 26 May 2006 11:06:19 -0300 (ADT) Received: (qmail 20708 invoked by uid 500); 26 May 2006 14:08:21 -0000 Date: Fri, 26 May 2006 09:08:20 -0500 From: Bruno Wolff III To: Andrew Sullivan Cc: pgsql-sql@postgresql.org Subject: Re: PLEASE help ME , HOW TO GENERATE PRIMARY Keys on the fly Message-ID: <20060526140820.GA17450@wolff.to> Mail-Followup-To: Bruno Wolff III , Andrew Sullivan , pgsql-sql@postgresql.org References: <20060526101207.5C4B35B0B83@svr4.postgresql.org> <20060526105037.GB5492@phlogiston.dyndns.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20060526105037.GB5492@phlogiston.dyndns.org> User-Agent: Mutt/1.4.2.1i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/193 X-Sequence-Number: 25224 On Fri, May 26, 2006 at 06:50:37 -0400, Andrew Sullivan wrote: > On Fri, May 26, 2006 at 05:11:26PM +0700, andi wrote: > > select rank() over(order by testeridpk ) as rank , * from tester; > > > > I get the result is like this, > > > > > > RANK TESTERIDPK TESTER_NAME > > > > 1 10 TESSS > > > > 2 90 NAMAAA > > > > 3 100 UUUUUUUU > > > > > > How in postgres sql I get the same result , please help me, because iam > > really frustating with this duty. The simplest solution is to add the rank information in your application as it reads the result set. > There's no built in for that that I know of. You could use a > temporary sequence to do it: > > BEGIN; > CREATE SEQUENCE tempseq; > SELECT nextval('tempseq') as rank, testeridpk, tester_name FROM testers > ORDER BY testeridpk; > ROLLBACK; > > which, I _think_, will get you what you want (i.e. that's not > tested). The ROLLBACK is just there to clean up the sequence. Rollbacks will not reset sequence values. Use setval to do that.