Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bsNvk-0004Wv-CD for pgsql-sql@arkaria.postgresql.org; Fri, 07 Oct 2016 05:42:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bsNvj-0002lh-QT for pgsql-sql@arkaria.postgresql.org; Fri, 07 Oct 2016 05:42:47 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bsNvi-0002kG-0L for pgsql-sql@postgresql.org; Fri, 07 Oct 2016 05:42:46 +0000 Received: from [195.159.176.226] (helo=blaine.gmane.org) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bsNva-0006p7-SI for pgsql-sql@postgresql.org; Fri, 07 Oct 2016 05:42:44 +0000 Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1bsNvS-0008GL-Va for pgsql-sql@postgresql.org; Fri, 07 Oct 2016 07:42:30 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: Re: row number with in cursor Date: Fri, 7 Oct 2016 07:42:32 +0200 Lines: 24 Message-ID: References: <81027780-1d91-4d1a-a1ef-7a6cfc77b37b@aklaver.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@blaine.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 In-Reply-To: X-Host-Lookup-Failed: Reverse DNS lookup failed for 195.159.176.226 (failed) X-Pg-Spam-Score: -1.1 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Kevin Duffy schrieb am 07.10.2016 um 03:01: > The cursor is defined as follows: > > currDR CURSOR (r character(15), sD date ) IS > select rate_key, rate_date, rate_value, > LAG(rate_date, 1 , null) OVER w as lag_r_date , > LAG(rate_value, 1, null) Over w as lag_r_value , > ( LAG(rate_value, 1, null) Over w /100 * ( rate_date - LAG(rate_date, 1 , null) OVER w ) ) / 360 as overnight_rate_return > from rate_quote > where rate_key = r and rate_date >= sD > WINDOW w as (PARTITION BY rate_key order by rate_date ROWS 1 PRECEDING ) > order by 2 ; > > Cursor works fine > Could I add a row number to the above? Sure, just add: ROW_NUMBER() OVER W AS RN As the overall result is ordered by the same expression as the window w, this should work without much overhead. Thomas -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql