1
我想這個查詢在交叉表的結果:交叉表查詢
SELECT district, sex ,count(sex)
FROM table1
GROUP BY sex, district
ORDER BY district;
district | sex | count
---------+-----+-----
dis_1 | M | 2
dis_1 | F | 4
dis_1 | NI | 1
dis_2 | M | 5
dis_2 | F | 2
像這樣:
district | M | F | NI
---------+---+---+---
dis_1 | 2 | 4 | 1
dis_2 | 5 | 2 | 0
我也沒有成功的一些測試,下面的查詢:
SELECT row_name AS district,
category_1::varchar(10) AS m,
category_2::varchar(10) AS f,
category_3::varchar(10) AS ni,
category_4::int AS count
FROM crosstab('select district, sex, count(*)
from table1 group by district, sex')
AS ct (row_name varchar(27),
category_1 varchar(10),
category_2 varchar(10),
category_3 varchar(10),
category_4 int);
謝謝,這就是它! – 2013-03-12 11:33:22