简体   繁体   English

Postgres中的交叉表函数不返回表中的任何值

[英]Crosstab function in postgres not returning any values in table

I'm trying to create a pivot table in pgadmin 4 using the crosstab function. 我正在尝试使用交叉表功能在pgadmin 4中创建数据透视表。 I've successfully created one by copying code (see below) from a postgres manual just to make sure the tablefunc extension was installed / able to work correctly: 我已经通过从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 |      |     |     |     |     |     |     |     |     |      |

however, when trying to import my own data, the table returns blank values: 但是,当尝试导入我自己的数据时,该表返回空白值:

select *
from medclaims2

output: 输出:

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)
);

output: 输出:

 member ICD9-1  ICD9-2  ICD9-4  ICD9-5
    1               
    2               
    3               
    4       

Any help would be much appreciated, thanks! 任何帮助将不胜感激,谢谢!

Is it just a typo? 这只是一个错字吗?

Paid_Amouint 

vs.

Paid_Amount

Regards Ralph 问候拉尔夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM