financial
金融领域聚合函数
仓库
intgr/pg_financial
https://github.com/intgr/pg_financial
源码
pg_financial-1.0.1.tar.gz
pg_financial-1.0.1.tar.gz
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
pg_financial | 1.0.1 | FUNC | PostgreSQL | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 4840 | financial | 否 | 是 | 否 | 是 | 否 | 是 | - |
| 相关扩展 | topn quantile lower_quantile count_distinct omnisketch ddsketch tdigest first_last_agg |
|---|
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.0.1 | 1817161514 | pg_financial | - |
| RPM | PIGSTY | 1.0.1 | 1817161514 | pg_financial_$v | - |
| DEB | PIGSTY | 1.0.1 | 1817161514 | postgresql-$v-pg-financial | - |
构建
您可以使用 pig build 命令构建 pg_financial 扩展的 RPM / DEB 包:
pig build pkg pg_financial # 构建 RPM / DEB 包
安装
您可以直接安装 pg_financial 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:
pig repo add pgsql -u # 添加仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install pg_financial; # 当前活跃 PG 版本安装
pig ext install -y pg_financial -v 18 # PG 18
pig ext install -y pg_financial -v 17 # PG 17
pig ext install -y pg_financial -v 16 # PG 16
pig ext install -y pg_financial -v 15 # PG 15
pig ext install -y pg_financial -v 14 # PG 14
dnf install -y pg_financial_18 # PG 18
dnf install -y pg_financial_17 # PG 17
dnf install -y pg_financial_16 # PG 16
dnf install -y pg_financial_15 # PG 15
dnf install -y pg_financial_14 # PG 14
apt install -y postgresql-18-pg-financial # PG 18
apt install -y postgresql-17-pg-financial # PG 17
apt install -y postgresql-16-pg-financial # PG 16
apt install -y postgresql-15-pg-financial # PG 15
apt install -y postgresql-14-pg-financial # PG 14
创建扩展:
CREATE EXTENSION financial;
用法
提供 XIRR(不规则内部收益率)聚合函数,类似于电子表格程序中的 XIRR 函数。
CREATE EXTENSION financial;
函数
| 函数 | 说明 |
|---|---|
xirr(amount float8, date timestamptz [, guess float8] ORDER BY date) | 计算不规则内部收益率 |
示例
-- 基本 XIRR 计算
SELECT xirr(amount, time ORDER BY time) FROM transaction;
-- 0.0176201237088334
-- 使用显式初始猜测值(Excel 兼容的默认值为 0.1)
SELECT xirr(amount, time, 0.1 ORDER BY time) FROM transaction;
-- 按投资组合计算 XIRR
SELECT portfolio, xirr(amount, time ORDER BY time)
FROM transaction
GROUP BY portfolio;
-- 作为窗口函数使用
SELECT xirr(amount, time) OVER (ORDER BY time)
FROM transaction;
当牛顿法无法收敛时返回 NULL。提供更好的猜测值在某些情况下可能有所帮助。