postgres中的交叉表函数不返回表中的任何值
Posted
技术标签:
【中文标题】postgres中的交叉表函数不返回表中的任何值【英文标题】:Crosstab function in postgres not returning any values in table 【发布时间】:2016-12-02 22:37:18 【问题描述】:我正在尝试使用 crosstab 函数在 pgadmin 4 中创建一个数据透视表。我已经通过从 postgres 手册中复制代码(见下文)成功地创建了一个,只是为了确保 tablefunc 扩展已安装/能够正常工作:
create table sales(year int, month int, qty int);
insert into sales values(2007, 1, 1000);
insert into sales values(2007, 2, 1500);
insert into sales values(2007, 7, 500);
insert into sales values(2007, 11, 1500);
insert into sales values(2007, 12, 2000);
insert into sales values(2008, 1, 1000);
SELECT * FROM crosstab(
$$ SELECT year, month, qty FROM sales ORDER BY 1 $$,
$$ SELECT m FROM generate_series(1,12) m $$
) AS (
year int,
"Jan" int,
"Feb" int,
"Mar" int,
"Apr" int,
"May" int,
"Jun" int,
"Jul" int,
"Aug" int,
"Sep" int,
"Oct" int,
"Nov" int,
"Dec" int
);
year | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec
------+------+------+-----+-----+-----+-----+-----+-----+-----+-----+------+------
2007 | 1000 | 1500 | | | | | 500 | | | | 1500 | 2000
2008 | 1000 | | | | | | | | | | |
但是,当尝试导入我自己的数据时,该表返回空白值:
select *
from medclaims2
输出:
member date paid_amount icd9
1 7/30/2011 43075 ICD9-1
2 8/16/2011 24895 ICD9-2
3 3/7/2012 18957 ICD9-4
4 8/16/2011 37476 ICD9-5
SELECT * FROM crosstab(
$$ SELECT member, icd9, paid_amount FROM medclaims2 $$,
$$ SELECT m FROM generate_series(1,4) m $$
) AS (
member varchar(25), "ICD9-1" varchar(25),"ICD9-2" varchar(25),"ICD9-4" int,"ICD9-5" varchar(25)
);
输出:
member ICD9-1 ICD9-2 ICD9-4 ICD9-5
1
2
3
4
任何帮助将不胜感激,谢谢!
【问题讨论】:
【参考方案1】:这只是一个错字吗?
Paid_Amouint
对比
Paid_Amount
问候 拉尔夫
【讨论】:
以上是关于postgres中的交叉表函数不返回表中的任何值的主要内容,如果未能解决你的问题,请参考以下文章
如何插入选择查询的结果(如果不返回任何行则默认)作为 Postgres 函数的一部分?
如何在 postgres 中使用“更新跳过锁定”而不锁定查询中使用的所有表中的行?