0
我試圖使用交叉表函數在pgadmin 4中創建數據透視表。我已經成功地創建了一個由一個Postgres手動複製代碼(見下文),以確保公正的tablefunc擴展安裝/能夠正常工作:但是postgres中的交叉表函數不返回表中的任何值
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
任何幫助將不勝感激,謝謝!
哎呀那只是一個錯字在我的帖子 - 謝謝! – JF1141