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

时间:2016-12-02 22:37:19

标签: sql postgresql pivot crosstab pgadmin

我正在尝试使用交叉表功能在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 个答案:

答案 0 :(得分:0)

这只是一个错字吗?

Paid_Amouint 

VS。

Paid_Amount

此致 拉尔夫