我有一个包含jsonb
列details
的PostgreSQL表.该列的一个字段包含名为ranges
的数组,其中每个元素都可以包含十进制值的数据.我需要将所有这些值转换为整数(即,将这些值乘以jsonb
)并删除键unit
.但是,这些字段也可以为空.该数组可以是任何大小.
我的jsonb
个例子:
{
"ranges": [
{
"types": {...},
"areaRange": {
"unit": "SQUARE",
"range": {
"to": 25.399999618530273,
"from": 24.360000610351562
}
}
},
{
"types": {...},
"areaRange": {
"unit": "SQUARE",
"range": {
"to": 44.560001373291016,
"from": 31.969999313354492
}
}
},
{
"types": {...},
"areaRange": {
"unit": "SQUARE",
"range": {
"to": null,
"from": null
}
}
},
{
"areaRange": {
"unit": "SQUARE",
"range": null
}
},
{
"areaRange": null
}
],
"...": {...}
}
我编写了一个取值为to
的查询,但它取回的是索引为0的数组中的一个特定元素.可以循环遍历数组的所有元素吗?如何立即更改此值?现在,在我的请求中,我只是得到了它.也许有更好的方法来解决我的问题?
select details -> 'ranges' -> 0 -> 'areaRange' -> 'range' -> 'to'
from table_name
where details -> 'ranges' -> 0 -> 'areaRange' -> 'range' -> 'to' is not null
四舍五入并乘以100的函数:
SELECT round( CAST(float8 '99.69999694824219' as numeric), 2) * 100; -- 9970