我在 PostgreSQL 12 中进行透视时遇到问题。
我必须使用一张构造笨拙的 table 。
这是一个类似于我创建的表(仅用于简单表示)-
CREATE TABLE test(Product_num INT, SN INT, Attribute VARCHAR(100), Value DECIMAL, Note VARCHAR(50) );
下一步是使用 COPY 功能导入 CSV 文件 -COPY public.test from 'C:\File Location\test.csv' DELIMITER ',' csv HEADER;
我得到一个超过 30k 行的表,看起来像 -Product_num | SN | Attribute | Value | Note |
100 9225 Unit sold 50 USA
100 9225 Unit price 4.99
100 9225 Num_boxes 2.5
101 9226 Unit sold 1 GER
101 9226 Unit price 920
101 9226 Num_boxes 2
我想要一张 table ,就像下一张 table -Product_num | SN | Unit Sold | Unit price | Num_boxes | Note
100 | 9225 | 50 | 4.99 | 2.5 | USA
101 | 9226 | 1 | 920 | 2 | GER
我尝试了多种方法,包括 crosstab() 并尝试聚合一些列,但遇到了一些问题。该表有一些需要考虑的问题——
我希望我能尽可能好地解释自己
谢谢
最佳答案
我会将所有属性聚合到一个 JSON 结构中,然后在最终查询中提取它们:
select product_num, sn,
(attributes ->> 'Num_boxes')::decimal as num_boxes,
(attributes ->> 'Unit sold')::decimal as unit_sold,
(attributes ->> 'Unit price')::decimal as unit_price,
note
from (
select product_num, sn, max(note) as note,
jsonb_object_agg(attribute, value) as attributes
from test
group by product_num, sn
) t
order by product_num, sn;
如果添加了新属性,您将需要扩展外部 SELECT 列表以反射(reflect)这一点。您无法在不更改查询的情况下获得动态的列列表。在 SQL 中,在运行语句之前必须知道查询的列数和类型。
关于postgresql - 使用 PostgreSQL 的 PIVOT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63466824/