我有一個表稱爲測試,它具有四列,如下所示。PostgreSQL 9.3:交叉表查詢
create table testing
(
cola varchar(10),
colb varchar(10),
colc varchar(10),
cold varchar(10)
);
插入一些行:
insert into testing values('A1','City1','X1','Z1'),
('A2','City2','X2','Z2'),
('A3','City3','X3','Z3'),
('A4','City4','X4','Z4');
預期結果:我想表明它看起來像如下所示的結果:
cola City1 City2 City3 City4 colc cold
--------------------------------------------
A1 1 0 0 0 X1 Z1
A2 0 1 0 0 X2 Z2
A3 0 0 1 0 X3 Z3
A4 0 0 0 1 X4 Z4
我嘗試:
我試着用下面的腳本,但得到一個錯誤。
select * from crosstab
(
'select cola,colb,count(colb),colc,cold from testing group by 1,2,4,5 order by 1,2,4,5',
'select distinct colb from testing'
)
as t
(
cola varchar(10),City1 integer,City2 integer,City3 integer,City4 integer,colc varchar(10),cold varchar(10)
);
錯誤詳細信息:
ERROR: invalid input syntax for integer: "City1"
什麼'A1'和'City1'之間的關係? – Kokizzu 2015-02-11 07:37:50
@Kokizzu,這只是一個例子。我可以說'A1'是產品代碼,'City1'是已售完的城市。 – MAK 2015-02-11 07:40:54