Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gddHL-0000U6-TM for pgsql-general@arkaria.postgresql.org; Sun, 30 Dec 2018 15:45:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gddHK-0003y2-Bt for pgsql-general@arkaria.postgresql.org; Sun, 30 Dec 2018 15:45:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gddHJ-0003uG-Ee for pgsql-general@lists.postgresql.org; Sun, 30 Dec 2018 15:45:25 +0000 Received: from squeakycode.net ([96.126.114.205]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gddH4-0001gR-Ot for pgsql-general@postgresql.org; Sun, 30 Dec 2018 15:45:24 +0000 Received: from rogue.localdomain (209-252-174-23.cgn.imoncommunications.net [209.252.174.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: andy) by squeakycode.net (Postfix) with ESMTPSA id 8095622825 for ; Sun, 30 Dec 2018 09:45:09 -0600 (CST) From: Andy Colson Subject: Re: Recursive CTE To: pgsql-general@postgresql.org References: Message-ID: <8c11bb53-5596-c24f-e997-1485123a2026@squeakycode.net> Date: Sun, 30 Dec 2018 09:45:07 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/29/18 12:34 PM, Glenn Schultz wrote: > All, > Following my earlier post on variable instantiation, I rethought how I was working with dates and realized I can fix the date and use static interval.  I came up with this recursive CTE which is the end goal.  However, the problem is that the convexity query cannot be used as a subquery.  So I think I need to use a join of convexity on the original query - not sure I am little stuck at this point but I feel I am close.  Any help would be appreciated. > > -Glenn If you are trying to find month summaries, then maybe date_trunc would be easier: select date_trunc('month', fctrdt), round(smmtocpr(cast(sum(currrpb * case when fnmloan.fctrdt = fnmloan_data.fctrdt then fnmloan_data.event else 0 end)/ sum(currrpb) as numeric) * 100),4) * 100 as "CPR 3mo" from fnmloan group by date_trunc('month', fctrdt)