pgsql将字符串转为整数和浮点数

如 final_value 字段为 varchar 字符串类型,需先转为浮点型,再求平均值

① final_value 有小数点,字符串转浮点数

select final_value :: float from monitor_data limit 2;

 

②  final_value 为整数,才可以将字符串转整数

select final_value :: integer from monitor_data limit 2;

 

③ 求平均值(7天内某个时间段的平均值)

select avg(g.a) from
(select AVG(final_value :: float) as A from monitor_data where create_time >= '2022-02-24 08:00:00' and create_time < '2022-02-24 08:15:00'
union all
select avg(final_value :: float) as A from monitor_data where create_time >= '2022-02-23 08:00:00' and create_time < '2022-02-23 08:15:00'
union all
select avg(final_value :: float) as A from monitor_data where create_time >= '2022-02-22 08:00:00' and create_time < '2022-02-22 08:15:00'
union all
select avg(final_value :: float) as A from monitor_data where create_time >= '2022-02-21 08:00:00' and create_time < '2022-02-21 08:15:00'
union all
select avg(final_value :: float) as A from monitor_data where create_time >= '2022-02-20 08:00:00' and create_time < '2022-02-20 08:15:00'
union all
select avg(final_value :: float) as A from monitor_data where create_time >= '2022-02-19 08:00:00' and create_time < '2022-02-19 08:15:00'
union all
select avg(final_value :: float) as A from monitor_data where create_time >= '2022-02-18 08:00:00' and create_time < '2022-02-18 08:15:00' ) g

 

 

 

 

posted @ 2022-02-28 13:21  浅浅童鞋  阅读(5032)  评论(0编辑  收藏  举报