在PostgreSQL中按列合并SQL表
我找到了一种方法来合并具有一些公共列的表,并生成一个新表(或更新现有表) 我的PostgreSQL版本是9.2.24 我从一个已经解决的帖子开始: 在本文中,我们有两个表: 请求的产品在PostgreSQL中按列合并SQL表,sql,postgresql,merge,Sql,Postgresql,Merge,我找到了一种方法来合并具有一些公共列的表,并生成一个新表(或更新现有表) 我的PostgreSQL版本是9.2.24 我从一个已经解决的帖子开始: 在本文中,我们有两个表: 请求的产品 +---------+-----------+--------------+---------+ | orderId | productId | productDesc | prodQty | +---------+-----------+--------------+---------+ | order1
+---------+-----------+--------------+---------+
| orderId | productId | productDesc | prodQty |
+---------+-----------+--------------+---------+
| order1 | product1 | description1 | 1 |
| order2 | product2 | description2 | 2 |
| order2 | product3 | description3 | 5 |
| order2 | product4 | description4 | 6 |
+---------+-----------+--------------+---------+
+---------+------------+--------------+--------+
| orderId | materialId | materialDesc | matQty |
+---------+------------+--------------+--------+
| order1 | material1 | description4 | 3 |
| order1 | material2 | description5 | 6 |
| order1 | material3 | description6 | 2 |
| order2 | material4 | description7 | 8 |
+---------+------------+--------------+--------+
和用过的材料
+---------+-----------+--------------+---------+
| orderId | productId | productDesc | prodQty |
+---------+-----------+--------------+---------+
| order1 | product1 | description1 | 1 |
| order2 | product2 | description2 | 2 |
| order2 | product3 | description3 | 5 |
| order2 | product4 | description4 | 6 |
+---------+-----------+--------------+---------+
+---------+------------+--------------+--------+
| orderId | materialId | materialDesc | matQty |
+---------+------------+--------------+--------+
| order1 | material1 | description4 | 3 |
| order1 | material2 | description5 | 6 |
| order1 | material3 | description6 | 2 |
| order2 | material4 | description7 | 8 |
+---------+------------+--------------+--------+
我们得到了一种将两个表合并为一列的方法,即orderId
新情景
现在,假设您有一个新的场景,新的表有一些更改。我们称第一个请求的产品为新的
+-------+------------+------+-----------+---------+
| rowID | CustumerID | year | productId | prodQty |
+-------+------------+------+-----------+---------+
| 1 | c_1 | 2017 | product1 | 1 |
| 2 | c_1 | 2018 | product1 | 2 |
| 3 | c_1 | 2017 | product2 | 5 |
| 4 | c_1 | 2018 | product2 | 5 |
| 5 | c_2 | 2017 | product1 | 6 |
| 6 | c_2 | 2018 | product1 | 6 |
| 7 | c_2 | 2017 | product2 | 6 |
| 8 | c_2 | 2018 | product2 | 6 |
+-------+------------+------+-----------+---------+
SQL查询
CREATE TABLE required\u Products\u NEW(
rowID数字,
CustumerID VARCHAR(128),
年份数字,
productId VARCHAR(128),
产品数量数字
);
在请求的产品中插入新的
价值观
(1,'c_1',2017,'product1',1),
(2,'c_1',2018,'product1',2),
(3,'c_1',2017,'product2',5),
(4,'c_1',2018,'product2',5),
(5,'c_2',2017,'product1',6),
(6,'c_2',2018,'product1',6),
(7,'c_2',2017,'product2',6),
(8,'c_2',2018,'product2',6);
要获取表格请求的产品\u新产品
+-------+------------+------+-----------+---------+
| rowID | CustumerID | year | productId | prodQty |
+-------+------------+------+-----------+---------+
| 1 | c_1 | 2017 | product1 | 1 |
| 2 | c_1 | 2018 | product1 | 2 |
| 3 | c_1 | 2017 | product2 | 5 |
| 4 | c_1 | 2018 | product2 | 5 |
| 5 | c_2 | 2017 | product1 | 6 |
| 6 | c_2 | 2018 | product1 | 6 |
| 7 | c_2 | 2017 | product2 | 6 |
| 8 | c_2 | 2018 | product2 | 6 |
+-------+------------+------+-----------+---------+
我们可以在请求的\u Products\u NEW中看到,通过组合三列,CustumerID,年份,productId,每一行都是唯一的
现在假设我有关于这些行的新信息。假设我有一个新表,它有三个相同的列来标识每一行。我们称此表为产品满意度表
SQL查询
创建表产品满意度(
rowID数字,
CustumerID VARCHAR(128),
年份数字,
productId VARCHAR(128),
满意数值
);
插入到产品中
价值观
(1,'c_1',2017,'product1',8),
(2,'c_1',2018,'product1',2),
(3,'c_1',2017,'product2',1),
(4,'c_2',2017,'product1',5),
(5,'c_2',2018,'product1',7),
(6,'c_2',2017,'product2',2),
(7,'c_2',2018,'product2',8),
(8,'c_2',2014,'product2',4),
(9,'c_1',2020,'product1',9);
要获得此表产品满意度
+-------+------------+------+-----------+-------------+
| rowID | CustumerID | year | productId | Satisfation |
+-------+------------+------+-----------+-------------+
| 1 | c_1 | 2017 | product1 | 8 |
| 2 | c_1 | 2018 | product1 | 2 |
| 3 | c_1 | 2017 | product2 | 1 |
| 4 | c_2 | 2017 | product1 | 5 |
| 5 | c_2 | 2018 | product1 | 7 |
| 6 | c_2 | 2017 | product2 | 2 |
| 7 | c_2 | 2018 | product2 | 8 |
| 8 | c_2 | 2014 | product2 | 4 |
| 9 | c_1 | 2020 | product1 | 9 |
+-------+------------+------+-----------+-------------+
您可以看到,在最后一个表中,我有一些与第一个表相关的满意度信息,但并非所有行都与第一个表匹配。例如,由于客户ID、年份和产品ID是相同的(c_1,2017,产品1),所以第一行的产品满意度(rowID=1)与第一行的请求的产品\u NEW(rowID=1)匹配。
但第四行(rowID=4)的要求的新产品(c_1,2018,产品2)在产品满意度方面不匹配。在自然语言中,我们没有任何关于客户2对2018年购买的产品2满意度的信息
同样的事情也可能发生在相反的意义上。例如,我们有客户2对2014年购买的产品2的满意度信息,客户1对2020年购买的产品1的满意度信息,但对于这些年,客户和产品,我们没有“数量信息”(“数量信息”在第一个表中要求的产品\u新的)。换句话说,我们无法将表产品满意度的rowID 7和rowID 8的信息与表请求的新产品相匹配
在本文中,我想将这些表合并到一个表中,以便从这两个表中获取所有信息。我需要把它放在一个表中进行回归分析。如果可能,我更喜欢使用合并步骤的结果的新列和行来更新现有表(例如,Requested\u Products\u NEW)
这里是预期的输出,它是一个包含10行的表。我删除了rowID,因为它与此无关(我在这里添加rowID是为了解释我的目的,但我的数据中并没有这一列):
谢谢你的帮助!谢谢大家 如果我理解正确,您需要一个
完全连接
:
select *
from product_satisfaction ps full join
Requested_Products_NEW rp
using (customer_id, product_id, year)
与您的问题无关,但是:Postgres 9.2是您应该尽快计划升级。我知道,我必须更新它。非常感谢。嗨,谢谢你的回答。我以前尝试过完全外部连接,但我遇到了一些问题,完全连接似乎更合适,这可能是我需要的。但是,我仍然有一个问题。我有90个变量要合并到一个表中。但是,在某个时刻,我的桌子变得非常大,比它应该的大10倍。我原以为是我的外部连接出了问题,但最后,我的数据模型可能有问题,可能是重复的。但我仍然不明白,为什么有些复制品可以推动数百万行而不是700或80亿行。谢谢你,谢谢你,戈登·林诺夫,你帮我指出了我真正的问题。我的模型数据中有大约20-30个副本,当应用完全联接时,这些副本会导致数百万个!现在一切都好了,我需要的是完整的连接。多谢各位