PostgreSQL: Common table expressions and Lateral Join
<p>I would like to implement this: </p>

<pre><code>with Period as
(
SELECT 
    dp.dtIni as dtRefPeriod,
    dp.dtEnd - p.days * interval '1 day' as dtIniWindow,
    dp.dtEnd,
    p.days
FROM public.vwDimPeriod dp
LEFT JOIN LATERAL ( select cast(vlParam as decimal(18,4)) as days
                    from public.DBParam
                    where cdparam = 'LifeTimeValueDays' ) p on TRUE
WHERE dp.dtEnd &lt; CURRENT_DATE
)
,Orders as
(       
select 
    p.dtRefPeriod,
    o.nmEmail,
    dtOrder,
    p.dtIniWindow,
    p.dtEnd,
    o.cdOrder,
    o.vlOrder,
    p.days
from 
    public.vwFactOrder o
LEFT JOIN LATERAL Period p on TRUE
where
    ltrim(rtrim(o.nmEmail)) &lt;&gt; ''
    and o.blValid = B'1'
)
select * from Orders;
</code></pre>

<p>And I am getting an error: </p>

<blockquote>
  <p>ERROR:  syntax error at or near "p" LINE 1: ...rom public.vwFactOrder
  o LEFT JOIN LATERAL Period p on TRUE ...
                                                               ^ (execution time: 219 ms; total time: 437 ms)</p>
</blockquote>