Postgresql – 从整数字段中减去当前年份以填充计算字段

huangapple go评论53阅读模式
英文:

Postgresql - subtracting current year from integer field to populate a calculated field

问题

I have been working on this for longer than I care to admit. I am needing to subtract an integer field (ex. 1960) from the current year (as an int) and have the results put into a calculated field. I have worked out the following select statement which does return the correct answer.

当将选择语句添加为“始终生成”,则会引发以下错误。

subtracting two integers into a calculated field should be fairly simple...and I am lost as to why this is not working....

英文:

Using PostgreSQL 12. I have been working on this for longer than I care to admit. I am needing to subtract an integer field (ex. 1960) from the current year (as an int) and have the results put into a calculated field. I have worked out the following select statement which does return the correct answer.

Postgresql – 从整数字段中减去当前年份以填充计算字段.

When adding the select statement as a 'generate always' the following error is thrown.

Postgresql – 从整数字段中减去当前年份以填充计算字段

subtracting two integers into a calculated field should be fairly simple...and I am lost as to why this is not working....

答案1

得分: 0

PostgreSQL目前仅实现了存储 生成列(不可变),而不是虚拟(可变,根据您的特定情况需要),考虑到虚拟生成列在读取时计算,而虚拟生成列类似于视图。

那么,您可以像这样将您的计算列实现为视图:

create view amp.amp_pumpstations_vw as 
select *, 
date_part('year', current_date)- inservice as asset_age
from amp.amp_pumpstations ap;
英文:

Since PostgreSQL currently implements only stored generated columns (immutable) and not virtual (mutable, as needed for your particular case), considering that a virtual generated column is computed when it is read and also a virtual generated column is similar to a view.

Then you could implement your calculated column as a view like this:

create view amp.amp_pumpstations_vw as 
select *, 
date_part('year', current_date)- inservice as asset_age
from amp.amp_pumpstations ap;

huangapple
  • 本文由 发表于 2023年6月13日 01:23:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定