database - 如何在 Postgresql 中创建返回动态列名的查询?

标签 database postgresql crosstab

我在报告数据库中有两张表,一张用于订单,一张用于订单项目。每个订单可以有多个订单项目,以及每个项目的数量:

Orders
+----------+---------+
| order_id | email   |
+----------+---------+
| 1        | 1@1.com |
+----------+---------+
| 2        | 2@2.com |
+----------+---------+
| 3        | 3@3.com |
+----------+---------+

Order Items
+---------------+----------+----------+--------------+
| order_item_id | order_id | quantity | product_name |
+---------------+----------+----------+--------------+
| 1             | 1        | 1        | Tee Shirt    |
+---------------+----------+----------+--------------+
| 2             | 1        | 3        | Jeans        |
+---------------+----------+----------+--------------+
| 3             | 1        | 1        | Hat          |
+---------------+----------+----------+--------------+
| 4             | 2        | 2        | Tee Shirt    |
+---------------+----------+----------+--------------+
| 5             | 3        | 3        | Tee Shirt    |
+---------------+----------+----------+--------------+
| 6             | 3        | 1        | Jeans        |
+---------------+----------+----------+--------------+

出于报告目的,我希望将这些数据反规范化为一个单独的 PostgreSQL View (或者只是运行一个查询),将上面的数据变成如下所示:

+----------+---------+-----------+-------+-----+
| order_id | email   | Tee Shirt | Jeans | Hat |
+----------+---------+-----------+-------+-----+
| 1        | 1@1.com | 1         | 3     | 1   |
+----------+---------+-----------+-------+-----+
| 2        | 2@2.com | 2         | 0     | 0   |
+----------+---------+-----------+-------+-----+
| 3        | 3@3.com | 3         | 1     | 0   |
+----------+---------+-----------+-------+-----+

即,它是订单中每个项目的数量与产品名称的总和;并将产品名称设置为列标题。我是否需要使用交叉表之类的东西来执行此操作,或者是否有使用子查询的巧妙方法,即使我在查询运行之前不知道不同产品名称的列表。

最佳答案

这是一个可能的答案:

create table orders 
(
  orders_id int PRIMARY KEY, 
  email text NOT NULL
);

create table orders_items 
(
  order_item_id int PRIMARY KEY, 
  orders_id int REFERENCES orders(orders_id) NOT NULL, 
  quantity int NOT NULL, 
  product_name text NOT NULL
);

insert into orders VALUES (1, '1@1.com');
insert into orders VALUES (2, '2@2.com');
insert into orders VALUES (3, '3@3.com');

insert into orders_items VALUES (1,1,1,'T-Shirt');
insert into orders_items VALUES (2,1,3,'Jeans');
insert into orders_items VALUES (3,1,1,'Hat');
insert into orders_items VALUES (4,2,2,'T-Shirt');
insert into orders_items VALUES (5,3,3,'T-Shirt');
insert into orders_items VALUES (6,3,1,'Jeans');


select 
  orders.orders_id, 
  email,
  COALESCE(tshirt.quantity, 0) as "T-Shirts",
  COALESCE(jeans.quantity,0) as "Jeans",
  COALESCE(hat.quantity, 0) as "Hats"
from 
  orders 
  left join (select orders_id, quantity from orders_items where product_name = 'T-Shirt') 
    as tshirt ON (tshirt.orders_id = orders.orders_id)
  left join (select orders_id, quantity from orders_items where product_name = 'Jeans') 
    as jeans ON (jeans.orders_id = orders.orders_id)
  left join (select orders_id, quantity from orders_items where product_name = 'Hat') 
    as hat ON (hat.orders_id = orders.orders_id)
;

使用 postgresql 测试。结果:

 orders_id |  email  | T-Shirts | Jeans | Hats 
-----------+---------+----------+-------+------
         1 | 1@1.com |        1 |     3 |    1
         2 | 2@2.com |        2 |     0 |    0
         3 | 3@3.com |        3 |     1 |    0
(3 rows)

根据您的意见,您可以尝试使用tablefunc像这样:

CREATE EXTENSION tablefunc;

SELECT * FROM crosstab
(
  'SELECT orders_id, product_name, quantity FROM orders_items ORDER BY 1',
  'SELECT DISTINCT product_name FROM orders_items ORDER BY 1'
)
AS
(
       orders_id text,
       TShirt text,
       Jeans text,
       Hat text
);

但我认为您对 SQL 的思考方式有误。你通常知道你想要哪些行并且必须告诉它 SQL。 “旋转表”90 度不是 SQL 的一部分,应该避免。

关于database - 如何在 Postgresql 中创建返回动态列名的查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46697537/

相关文章:

php - Symfony Doctrine 和存储过程

php - `pg_query_params` 和参数太多

sql - 从 PostgreSQL 到 SAS

sql - 使用 postgres tablefunc crosstab() 计算错误答案

sql - 将两个表中的几列合并为一行,分别为前一个表中的id

php - 使用ajax根据下拉菜单选择更新文本字段

mysql - 将 Visual Studio 连接到 MySQL

php - 动态增加的可行性。数据库中的表和行

r - 当交叉表中的行和列都是分类名称时,如何指定行和列名称

mysql - 一种数据库结构包含多个数据库,还是一个全局数据库?