Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bsJrB-0003zd-6q for pgsql-sql@arkaria.postgresql.org; Fri, 07 Oct 2016 01:21:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bsJrA-0005fd-GY for pgsql-sql@arkaria.postgresql.org; Fri, 07 Oct 2016 01:21:48 +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 1bsJr7-0005eQ-J4 for pgsql-sql@postgresql.org; Fri, 07 Oct 2016 01:21:45 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bsJr4-0002EO-Je for pgsql-sql@postgresql.org; Fri, 07 Oct 2016 01:21:44 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 6C6EA20638; Thu, 6 Oct 2016 21:21:41 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute3.internal (MEProxy); Thu, 06 Oct 2016 21:21:41 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=xtLcQ17u4LP32FxMrU3ooc6IOVQ=; b=JRX/eY iyFh/vSFcW8ibU7gmc1rNHB+q1EzlqSDe0v15YoulKO7oICCYr9vJyI7SnnYegPm AUM8InksKy8jGLIR8bu17XX9Q4CDrovsd5M2J1LgvEyQxhoJkmaOR8ssR+CJ5r9t 6vXj5IEgEMFqmCJjI14LHVakamnk8L7F+SBrM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=xtLcQ17u4LP32Fx MrU3ooc6IOVQ=; b=aXoVJnwWVuJ6yTJLeJf3g6JxGQ6CgkU2czjxbbgGg5COFSV 1vC6EylwRcDfU5rTt+5o4m3z2B/9Zzxu6Yjj8g8DzFHAKr7YeRx3fhCDnfiMaMWv jNppUXVHAnALvlGHte+1W3e0HwNOmbVSkTh+N1nW6+UXN9DBekMz2YXcPJBU= X-Sasl-enc: xFR5XYZOBJOiI/oFINby0UV4h1fzd2PrsUoG4xER95/R 1475803301 Received: from [192.168.1.2] (174-21-85-118.tukw.qwest.net [174.21.85.118]) by mail.messagingengine.com (Postfix) with ESMTPA id DF792F2985; Thu, 6 Oct 2016 21:21:40 -0400 (EDT) Subject: Re: row number with in cursor To: Kevin Duffy References: <81027780-1d91-4d1a-a1ef-7a6cfc77b37b@aklaver.com> Cc: pgsql-sql@postgresql.org From: Adrian Klaver Message-ID: <5040cfab-e430-ba0f-80e3-40f61623d1cf@aklaver.com> Date: Thu, 6 Oct 2016 18:21:40 -0700 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) 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 On 10/06/2016 06:01 PM, Kevin Duffy wrote: > 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? Heading out the door, but here is something: https://www.postgresql.org/docs/9.6/static/functions-window.html and a quick search using 'postgres row number': https://www.google.com/search?q=postgres+row+number&ie=utf-8&oe=utf-8 > > KD > > On Thu, Oct 6, 2016 at 8:26 PM, Adrian Klaver > wrote: > > On 10/06/2016 05:05 PM, Kevin Duffy wrote: > > Hello All: > > I need your kind assistance, to learn if it is possible > within a cursor to know what row you are on. > > > What version of Postgres? > > Are you actually using a CURSOR as defined by plpgsql?: > > https://www.postgresql.org/docs/9.6/static/plpgsql-cursors.html > > > Looks like you are LOOPing over the results of a function? > > Something like this: > > for currDateRate IN currDR( rate_in, start_date ) LOOP > > raise notice ' currDateRate.rate_date: %', > currDateRate.rate_date ; > raise notice ' currDateRate.lag_r_value: %', > currDateRate.lag_r_value ; > raise notice ' currDateRate.rate_value: %', > currDateRate.rate_value ; > raise notice ' currDateRate.overnight_r: %', > currDateRate.overnight_rate_return ; > tr_index := tr_index *( 1+ > currDateRate.overnight_rate_return ) ; > raise notice ' tr_index: %',tr_index ; > -- this does NOT work > raise notice ' row number %', currDateRate%ROWNUMBER ; > > > There is no ROWNUMBER in plpgsql(I am assuming you are using that?). > > > End LOOP; > > for testing purposes would like to break out after twenty records. > Yes I know I could do a simple counter like this: > > rtn_cnt := rtn_cnt +1; > if rtn_cnt >= 20 then > return rtn_cnt; > END IF; > > > I think you will need to use some variation of the above. > Or add an argument to currDR(assuming it is a function) that sets a > LIMIT. > > > > > thamks for your attention to this matter > > KD > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql