pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Recursive CTE
2+ messages / 2 participants
[nested] [flat]

* Recursive CTE
@ 2018-12-29 18:34  Glenn Schultz <glenn@bondlab.io>
  0 siblings, 1 reply; 2+ messages in thread

From: Glenn Schultz @ 2018-12-29 18:34 UTC (permalink / raw)
  To: pgsql-general

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

SET max_parallel_workers_per_gather = 8;
SET random_page_cost = 1;
SET enable_partitionwise_aggregate = on;
with recursive convexity (fctrdt, CPR3mo) as
(
select
cast((select max(fctrdt) - interval '1 month' from fnmloan) as date) as
"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
join
fnmloan_data
on
fnmloan_data.loanseqnum = fnmloan.loanseqnum
where
fnmloan.fctrdt between ((select max(fctrdt)-interval '1 month' from
fnmloan) - interval '2 month') and (select max(fctrdt) - interval '1 month'
from fnmloan)
and
fnmloan.poolprefix = 'CL'
union all
select
cast((select max(fctrdt) - interval '1 month' from convexity) as date) as
"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
join
fnmloan_data
on
fnmloan_data.loanseqnum = fnmloan.loanseqnum
where
fnmloan.fctrdt between ((select max(fctrdt)-interval '1 month' from
convexity) - interval '2 month') and (select max(fctrdt) - interval '1
month' from convexity)
and
fnmloan.poolprefix = 'CL'
and
convexity.fctrdt <= (select max(fctrdt) - interval' 12 months' from fnmloan)
)
select * from convexity

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Recursive CTE
@ 2018-12-30 15:45  Andy Colson <andy@squeakycode.net>
  parent: Glenn Schultz <glenn@bondlab.io>
  0 siblings, 0 replies; 2+ messages in thread

From: Andy Colson @ 2018-12-30 15:45 UTC (permalink / raw)
  To: pgsql-general

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)






^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2018-12-30 15:45 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-29 18:34 Recursive CTE Glenn Schultz <glenn@bondlab.io>
2018-12-30 15:45 ` Andy Colson <andy@squeakycode.net>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox