首页 > 解决方案 > 按天更新 postgresql 中的兴趣值

问题描述

我需要一些帮助,至少是一个提示,关于如何将这个在 Firebird 中工作的查询写入 PostgreSQL。这是关于每天自动更新的兴趣。一直以这种方式工作,但现在我们正在迁移,尽管我尝试了许多不同的方法,但我找不到解决方案。

execute block as 
declare variable id Integer;
begin 
for select BILL_ID from BILLS into :id do update BILLS set BILL_INTEREST_VALUE = 
TRUNC(replace(((((BILL_VALUE * BILL_PERCENT) / 100) / 30) * datediff(day, BILL_INTEREST_DATE, current_date)),',','.'), 2)where BILL_ID = :id and BILL_CLIENT_ID="+id+"
and datediff(day, BILL_INTEREST_DATE, current_date) >=  BILL_DAYS_INTEREST and 
BILL_VALUE > 0.00;
end;

标签: javapostgresqlfirebird

解决方案


我不明白为什么使用过程语言,据我所知,这可以替换为没有循环的单个 UPDATE 语句:

UPDATE BILLS 
  SET bill_interest_value = trunc( (((BILL_VALUE * BILL_PERCENT) / 100) / 30) * (current_date - bill_interest_date))
WHERE bill_id IN (SELECT bill_id FROM bills) --<< essentially useless
  AND bill_client_id = ?
  AND current_date - bill_interest_date >=  bill_days_interest 
  AND bill_value > 0.00;

以上假设bill_interest_date是一date列(不是时间戳)


推荐阅读