Postgres 层次结构输出

Postgres Hierarchy output

我正在努力研究如何使用层次结构查询获得正确的输出。

我有一个 table 每天加载所有产品及其价格。在一段时间内,这可以取消并再次激活。

我相信对于 oracle 我们可以使用 Connect By。

 WITH RECURSIVE cte AS (                                                                                                                                                          
select min(event_date) event_date, item_code,sum(price::numeric)/1024/1024 price, 1 AS level     
from rdpidevdat.raid_r_cbs_offer_accttype_map where product_type='cars'  and item_code in ('Renault') 
group by item_code
UNION  ALL  
SELECT e.event_date, e.item_code, e.price, cte.level + 1                                        
from (select event_date, item_code,sum(price::numeric)/1024/1024 price
from rdpidevdat.raid_r_cbs_offer_accttype_map where product_type='cars'  and item_code in ('9859') 
group by event_date,item_code) e join cte ON e.event_date = cte.event_date and e.item_code = cte.item_code
)                                                                                                                                                                                     
SELECT *                                                                                                                                                                                 
FROM   cte where item_code in ('Renault') ;

我如何将输出放在一段时间内每个产品的范围内?

如果我们有数据:

  EVENT_DATE   | ITEM_COD| PRICE
  20210910     | Renaut  | 2500
  20210915     | Renaut  | 2500
  20210920     | Renaut  | 2600
  20211020     | Renaut  | 2900
  20220101     | Renaut  | 2500

预期的输出应该是:

-------------------------------------------------
FROM_EVENT_DATE | TO_EVENT_DATE | ITEM_COD| PRICE
 20210910       |  20210915     | Renaut  | 2500
 20210915       |  20210920     | Renaut  | 2600
 20210920       |  20211020     | Renaut  | 2900
 20211020       |  20220101     | Renaut  | 2500

提前致谢并致以问候!

我已经找到了解决方案。使用 Lag 和 lastvalue 函数。无需使用层次结构一。