英文:
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.
When adding the select statement as a 'generate always' the following error is thrown.
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论