[英]Postgres interval in total months
I have a Postgres table with interval data, such as: 我有一个带有间隔数据的Postgres表,例如:
I would like to create a view which displays the values, but in total months (so the correct values in my case would be: 0, 1, 1.5, 9, 12). 我想创建一个显示值的视图,但以总月份为单位(因此,我的正确值是:0、1、1.5、9、12)。 I have tried using:
我试过使用:
EXTRACT(MONTHS FROM "MyTable"."Interval"::INTERVAL)
However, this yielded incorrect results (0, 1, 1 , 9, 0 ). 然而,这产生了不正确的结果(0,1,1,9,0)。
Is there another function, which would display the expected values? 是否还有另一个函数可以显示期望值?
Well, the extract
function retrieves subfields such as year or hour from date/time values, not the total time you want,but we can construct the result by ourself, as below: 好吧,
extract
函数从日期/时间值中检索子字段,例如年或小时,而不是所需的总时间,但是我们可以自己构造结果,如下所示:
postgres=# select * from month_table ;
month
------------
0 month
1 month
1.5 months
9 months
1 year
2 years
1.5 years
(7 rows)
postgres=# select
case when month ~ 'month' then regexp_replace(month,' month.*','')::numeric
when month ~ 'year' then regexp_replace(month,' year.*','')::numeric * 12 end as months
from
month_table;
months
--------
0
1
1.5
9
12
24
18.0
(7 rows)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.