Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4633FB5DBE3 for ; Thu, 12 May 2011 17:45:33 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 71208-04 for ; Thu, 12 May 2011 20:45:26 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 62F90B5DBC1 for ; Thu, 12 May 2011 17:45:26 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QKcl9-0003ma-TI for pgsql-sql@postgresql.org; Thu, 12 May 2011 22:45:23 +0200 Received: from ppp-93-104-164-108.dynamic.mnet-online.de ([93.104.164.108]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 12 May 2011 22:45:23 +0200 Received: from spam_eater by ppp-93-104-164-108.dynamic.mnet-online.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 12 May 2011 22:45:23 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: Re: ordering by date for each ID Date: Thu, 12 May 2011 22:45:21 +0200 Lines: 30 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ppp-93-104-164-108.dynamic.mnet-online.de User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 In-Reply-To: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, T_RP_MATCHES_RCVD=-0.01, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201105/86 X-Sequence-Number: 35370 Nguyen,Diep T wrote on 12.05.2011 03:59: > Each ID can have different number of score counts. > > My goal is to add column "order", which shows the order of the values > in column "date" in descendant order for each property. The expected output > will look like this: > > id | date | score_count | order > ----+------------------+---------------+----------- > 13 | 1999-09-16 | 4 | 4 > 13 | 2002-06-27 | 4 | 3 > 13 | 2006-10-25 | 4 | 2 > 13 | 2010-05-12 | 4 | 1 > 65 | 2002-07-18 | 3 | 3 > 65 | 2004-08-05 | 3 | 2 > 65 | 2007-08-15 | 3 | 1 > 86 | 2001-05-29 | 5 | 5 > 86 | 2002-04-04 | 5 | 4 > 86 | 2006-03-02 | 5 | 3 > 86 | 2008-02-13 | 5 | 2 > 86 | 2011-01-19 | 5 | 1 > > Any help would be appreciated. SELECT id, date, score_count, row_number() over (partition by id order by date desc) as order_value FROM your_table