gpt4 book ai didi

postgresql - 组内级联的Postgres交叉表(文本,文本)

转载 作者:行者123 更新时间:2023-11-29 12:16:53 28 4
gpt4 key购买 nike

表架构

DROP TABLE bla;
CREATE TABLE bla (id INTEGER, city INTEGER, year_ INTEGER, month_ INTEGER, val INTEGER);

数据

INSERT INTO bla VALUES(1, 1, 2017, 1, 10);
INSERT INTO bla VALUES(2, 1, 2017, 2, 20);
INSERT INTO bla VALUES(3, 1, 2017, 1, 15);
INSERT INTO bla VALUES(4, 1, 2017, 2, 5);
INSERT INTO bla VALUES(5, 2, 2017, 1, 10);
INSERT INTO bla VALUES(6, 2, 2017, 2, 15);
INSERT INTO bla VALUES(7, 1, 2018, 1, 10);
INSERT INTO bla VALUES(8, 1, 2018, 1, 10);

我试图将它们聚合起来并放入数据透视表格式中,这样对于每个 (city, year_) 组合,我都会有相应的总 val。以下是我可以从在线资源和官方文档中找到的内容。

SELECT * FROM crosstab (
'SELECT city, year_, month_, SUM(val) FROM bla GROUP BY 1, 2, 3 ORDER BY 1',
'SELECT DISTINCT month_ FROM bla ORDER BY 1'
) AS final_table (
city INTEGER,
year_ INTEGER,
january INTEGER,
February INTEGER
);

这是我现在得到的输出。

enter image description here

请注意与组 (city 1, year_ 2018) 对应的条目是如何丢失的。我还没有找到任何解决方案,并认为交叉表可能不支持这种级联结构。

我知道我可以创建一个临时变量 (city_year_) 来绕过这个问题。

SELECT * FROM crosstab (
'SELECT CONCAT(city, year_)::text AS tag, month_, SUM(val) FROM bla GROUP BY 1, 2 ORDER BY 1',
'SELECT DISTINCT month_ FROM bla ORDER BY 1'
) AS final_table (
tag text,
january INTEGER,
February INTEGER
);

在这里输出。

enter image description here

但是 cityyear_ 在各自的列中是我的首选格式(它在视觉上更加丰富并保留了原始数据 - 将 tag 变量拆分为cityyear_ 需要知道 tag 是如何定义的)。

非常感谢任何解决方法/帮助。问候。

最佳答案

Postgres 的crosstab() 期望源查询具有特定格式。

This statement [source sql] must return one row_name column, one category column, and one value column. It may also have one or more "extra" columns. The row_name column must be first. The category and value columns must be the last two columns, in that order. Any columns between row_name and category are treated as "extra". The "extra" columns are expected to be the same for all rows with the same row_name value.

这里的问题是您将 year_month_ 都作为 row_name 列,而 crosstab() 允许只有一个 row_name 列。因此,我们必须使用其他东西作为 row_name 列。让我们使用这个函数 dense_rank()

试试这个。

SELECT year_, city, january, february FROM crosstab (
'SELECT dense_rank() OVER (ORDER BY year_, city)::int AS row_name,
year_, city , month_, SUM(val) FROM bla GROUP BY city, year_, month_
ORDER BY 1',
'SELECT DISTINCT month_ FROM bla ORDER BY 1'
) AS final_table (
rowname integer,
year_ integer ,
city integer,
january INTEGER,
february INTEGER
);

这会产生所需的输出:

-------------------------------------
| year_ | city | january | february |
-------------------------------------
| 2017 | 1 | 25 | 25 |
-------------------------------------
| 2017 | 2 | 10 | 15 |
-------------------------------------
| 2018 | 1 | 20 | |
-------------------------------------

关于postgresql - 组内级联的Postgres交叉表(文本,文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48680978/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com