如何在PostgreSQL中执行级联舍入?

偶像小

级联舍入是一种在保留浮点数之和的同时舍入浮点数组的算法。如何在PostgreSQL中实现这一算法?

克林

您可以在plpgsql中实现此功能

create or replace function cascade_rounding(float[])
returns int[] immutable language plpgsql as $$
declare
    fp_total float = 0;
    int_total int = 0;
    fp_value float;
    int_value int;
    result int[];
begin
    foreach fp_value in array $1 loop
        int_value := round(fp_value + fp_total) - int_total;
        fp_total := fp_total + fp_value;
        int_total := int_total + int_value;
        result := result || int_value;
    end loop;
    return result;
end $$;

select cascade_rounding(array[1.1, 1.2, 1.4, 1.2, 1.3, 1.4, 1.4])

 cascade_rounding
------------------
 {1,1,2,1,1,2,1}
(1 row) 

尝试使用Db <> fiddle中的功能

更新。您可以将该功能应用于列。示例表:

create table my_table(id serial primary key, float_number float);
insert into my_table (float_number) 
select unnest(array[1.1, 1.2, 1.4, 1.2, 1.3, 1.4, 1.4])

查询:

select 
    unnest(array_agg(id order by id)) as id,
    unnest(array_agg(float_number order by id)) as float_number,
    unnest(cascade_rounding(array_agg(float_number order by id))) as int_number
from my_table;

但是,这不是一个完美的解决方案。该查询非常复杂且次优。

在Postgres中,您可以创建自定义聚合,以将其用作窗口函数。这不是特别困难,但是需要一些知识,请参阅文档中的用户定义的聚合

create type cr_type as (int_value int, fp_total float, int_total int);

create or replace function cr_state(state cr_type, fp_value float)
returns cr_type language plpgsql as $$
begin
    state.int_value := round(fp_value + state.fp_total) - state.int_total;
    state.fp_total := state.fp_total + fp_value;
    state.int_total := state.int_total + state.int_value;
    return state;
end $$;

create or replace function cr_final(state cr_type)
returns int language plpgsql as $$
declare
begin
    return state.int_value;
end $$;

create aggregate cascade_rounding_window(float) (
    sfunc = cr_state,
    stype = cr_type,
    finalfunc = cr_final,
    initcond = '(0, 0, 0)'
);

使用聚合作为窗口函数:

select 
    id, 
    float_number, 
    cascade_rounding_window(float_number) over (order by id) as int_number
from my_table;

 id | float_number | int_number
----+--------------+------------
  1 |          1.1 |          1
  2 |          1.2 |          1
  3 |          1.4 |          2
  4 |          1.2 |          1
  5 |          1.3 |          1
  6 |          1.4 |          2
  7 |          1.4 |          1
(7 rows)    

Db <>小提琴。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Postgresql中舍入时间?

来自分类Dev

如何在PostgreSQL中多次执行查询

来自分类Dev

如何在PostgreSQL中多次执行查询

来自分类Dev

如何在Objectify中实现级联删除?

来自分类Dev

如何在mysql中查看级联?

来自分类Dev

如何在级联中设置UIPickerView

来自分类Dev

如何在Linux中杀死级联?

来自分类Dev

级联如何在双向关系中工作

来自分类Dev

如何在excel中创建级联列表?

来自分类Dev

如何在UICollectionViewCell中对UIImageView进行舍入?

来自分类Dev

如何在Ruby中舍入零

来自分类Dev

如何在flutter中实现舍入的bottomAppbar?

来自分类Dev

如何在python中制作舍入函数

来自分类Dev

如何在SPARK SQL中舍入小数

来自分类Dev

如何在shell中舍入输出?

来自分类Dev

如何在 JQUERY 中舍入小数

来自分类Dev

如何在 ssrs 报告中停止舍入

来自分类Dev

如何在SQL中执行此操作(PostgreSQL窗口函数?)

来自分类Dev

如何在PostgreSQL中识别函数是否正在执行

来自分类Dev

如何在PostgreSQL 9.2.6 + CentO中执行/运行.sql文件

来自分类Dev

如何在 jpa nativequery 中执行 postgresql json 函数查询?

来自分类Dev

删除 PostgreSQL 中的级联条件

来自分类Dev

如何在mysql中添加删除级联选项?

来自分类Dev

如何在Laravel4中级联软删除?

来自分类Dev

如何在DataGridView控件中实现级联ComboBox?

来自分类Dev

如何在数据库中存储级联类别

来自分类Dev

如何在黑莓级联中拆分字符串

来自分类Dev

如何在Javascript / HTML中创建级联下拉列表?

来自分类Dev

如何在Fluent NHibernate中设置默认级联