且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 PostgreSQL 创建数据透视表

更新时间:2023-02-03 17:06:50

首先使用聚合函数 avg():

SELECT neighborhood, bedrooms, avg(price)
FROM   listings
GROUP  BY 1,2
ORDER  BY 1,2

然后按照相关答案中的详细说明将结果提供给 crosstab() 函数:

Then feed the result to the crosstab() function as instructed in great detail in this related answer: