我一直在尝试对多个枢纽和多个列进行交叉分析
我怎么去
create table sales(year int, branch text, month int, qty int, scrap int, yield int);
insert into sales values(2007, 'Houston', 1, 1000, 66, 11);
insert into sales values(2007, 'Houston', 3, 1500, 55, 22);
insert into sales values(2007, 'Austin', 3, 500, 44, 33);
insert into sales values(2007, 'Laredo', 2, 1500, 77, 44);
insert into sales values(2007, 'El Paso', 2, 2000, 88, 55);
insert into sales values(2008, 'Waco', 1, 900, 99, 66);
像这样交叉表
year | branch | jan_qty | jan_scr | jan_yld | feb_qty | feb_scr | feb_yld | mar_qty | mar_scr | mar_yld
------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------
2007 | Austin | | | | | | | 500 | 44 | 33
2007 | El Paso | | | | 2000 | 88 | 55 | | |
2007 | Houston | 1000 | 66 | 11 | | | | 1500 | 55 | 22
2007 | Laredo | | | | 1500 | 77 | 44 | | |
2008 | Waco | 900 | 99 | 66 | | | | | |
年份和分支都是支点,每个月包含3个值?
答案 0 :(得分:0)
1。最简单的路线
可能是以下情况。它不是 DRY ,但它可以完成此工作,而且可读性强,可用于任何SQL风格,并且可以轻松地由应用程序代码生成。
SELECT
grain.year,
grain.branch,
(SELECT qty FROM sales WHERE sales.year = grain.year AND sales.branch = grain.branch AND sales.month = 1) AS jan_qty,
(SELECT scr FROM sales WHERE sales.year = grain.year AND sales.branch = grain.branch AND sales.month = 1) AS jan_scr,
(SELECT yld FROM sales WHERE sales.year = grain.year AND sales.branch = grain.branch AND sales.month = 1) AS jan_yld,
... same for feb ...,
... same for mar ...
FROM (SELECT DISTINCT year, branch FROM sales) grain
应用程序代码只需要列jan_qty
,jan_scr
...的名称即可通过简单的循环生成此SQL。
我将确保EXPLAIN ANALYZE
以上查询来检查PostgreSQL是否有效地运行了该查询。
2。美丽的路线
这是一个数据透视表,PostgreSQL为我们提供了CROSSTAB函数来进行这种查询。 This post完美地解释了如何做到这一点,包括如何处理缺失值(即结果表中的空单元格),在您提供的示例中就是这种情况