postgresql - 将变量传递给 DO 循环
问题描述
我希望能够将 2 个变量传递给我的DO LOOP
(effectivedate
和nbr_mem_months
。这些变量当前被硬编码到DO LOOP
Declare 语句中。这个逻辑计算成员对健康计划有效的月数,并为相应的添加一个增量年和月桶。因此,对于查询的每一行,elan.elig
都会DO LOOP
将增量添加到YearMonth
桶中。因此,如果成员的生效日期201910131
和期限日期为20190531
,DO LOOP
则将为每个桶添加 1:201901 201902 201903 201904 201905
select (extract(year from age(case when terminationdate is null then
CURRENT_DATE else terminationdate END ,effectivedate ))) *12 +
(extract(month from age(case when terminationdate is null then
CURRENT_DATE else terminationdate END ,effectivedate )) +1)
as "effectivedate" ,to_char(effectivedate,'YYYYMM') as nbr_mem_months
from elan.elig
DO $$
declare
nbr_mem_months integer=5;
effectivedate date='2019-04-01';
ym char(6) =to_char(effectivedate,'YYYYMM');
begin
for r in 1..nbr_mem_months loop
update elan.pmpm set mbrmonths=mbrmonths+1 where yyyyymm=ym;
effectivedate=effectivedate + interval '1 month';
ym=to_char(effectivedate,'YYYYMM');
end loop;
end;
$$;
解决方案
推荐阅读
- c# - 包装在 WPF 中的 Chromium - 查找元素并调用操作
- java - 标记的黄瓜场景运行
- reactjs - 从节流函数内部调用类方法时节流不起作用
- arm - 汇编 stm r12,{r1,r4,r7} 指令触发的 ARM Cortex R5 异常
- ajax - 在 azure web 应用程序中使用 axios post/get 进行全日历时出现内部服务器错误 500
- javascript - 使用自动完成功能获取 API 数据并过滤搜索输入
- python - 为 RNN 使用转换时间戳
- ios - 打开XIB文件出现黑屏
- python - 使用 MySQL 从 Django QuerySet 中获取不同的值
- electron - 有没有办法提高 Electron 中 webview 的性能?