简体   繁体   English

连接两个表并合并一列中的文本值

[英]Join two tables and merge text values in one column

I have the following two tables which you can also find in the sql fiddle here :我有以下两个表,您也可以在此处sql fiddle找到

CREATE TABLE Sales (
    Sales_Date DATE,
    Product TEXT,
    Sales_Channel TEXT,
    Sales_Quantity VARCHAR(255)
);

INSERT INTO Sales
(Sales_Date, Product, Sales_Channel, Sales_Quantity)
VALUES 
("2017-05-23", "Product A", "Online", "400"),
("2018-09-10", "Product A", "Store", "200"),
("2018-12-14", "Product B", "Store", "600"),
("2019-01-03", "Product B", "Store", "700"),
("2019-02-15", "Product B", "Online", "650"),
("2019-03-20", "Product A", "Online", "380"),
("2019-08-25", "Product C", "TradeFair", "120"),
("2019-09-16", "Product C", "Online", "470"),
("2019-09-16", "Product A", "Store", "920"),
("2019-10-20", "Product B", "TraidFair", "860"),
("2020-01-03", "Product B", "TradeFair", "610");


CREATE TABLE Purchasing (
    Purchasing_Date VARCHAR(255),
    Product TEXT,
    Purchasing_Channel TEXT,
    Purchasing_Quantity VARCHAR(255)
);

INSERT INTO Purchasing
(Purchasing_Date, Product, Purchasing_Channel, Purchasing_Quantity)
VALUES 
("2017-01-10", "Product A", "Local_Supplier", "1000"),
("2017-01-16", "Product A", "Local_Supplier", "3000"),
("2017-01-19", "Product B", "Reseller", "1500"),
("2018-05-14", "Product B", "Reseller", "4500"),
("2018-05-14", "Product C", "Foreign_Import", "1800"),
("2019-04-16", "Product C", "Foreign_Import", "2300");

NOTE: Each product is assigned explicitly to one purchasing channel!注意:每个产品都明确分配到一个购买渠道!


Now, I want to make a query which gives me the following result:现在,我想做一个查询,它给我以下结果:

Sales_Date   Product      Channel                   Sales_Quantity
2017-05-23   Product A    Online_Local_Supplier     400 
2018-09-10   Product A    Store_Local_Supplier      200
2018-12-14   Product B    Store_Reseller            600
2019-01-03   Product B    Store_Reseller            650
:            :            :                         :
:            :            :                         :
:            :            :                         :

As you can see I want to merge the Sales_Channel and the Purchasing_Channel in one column.如您所见,我想将Sales_ChannelPurchasing_Channel合并在一列中。
Therefore, I set up the following query:因此,我设置了以下查询:

SELECT 
s.Sales_Date, 
s.Product, 
(Case sales_channel
 When "Online" Then "Online"
 When "Store" then "Store"
 When "TradeFair" then "Traidfair"
 ELSE "NoSalesChannel"
 END) AS Channel,
s.Sales_Quantity
FROM Sales s
JOIN Purchasing p ON p.Product = s.Product
GROUP BY 1,2;

This query inserts the Sales_Channel correctly but how do I have to change it so the Purchasing_Channel is added to it as displayed in my desired result?此查询正确插入Sales_Channel但我必须如何更改它以便将Purchasing_Channel添加到其中,如我想要的结果所示?

Try distinct :尝试distinct

SELECT distinct
s.Sales_Date, 
s.Product, 
concat(s.Sales_Channel, '_', p.Purchasing_Channel) as Chanel,
s.Sales_Quantity
FROM Sales s
JOIN Purchasing p ON p.Product = s.Product

Try concat and group by .尝试concatgroup by

SELECT 
s.Sales_Date, 
s.Product, 
concat(s.Sales_Channel, '_', p.Purchasing_Channel) as Chanel,
s.Sales_Quantity
FROM Sales s
JOIN Purchasing p ON p.Product = s.Product
group by s.Sales_Date, 
s.Product,
concat(s.Sales_Channel, '_', p.Purchasing_Channel),
s.Sales_Quantity

This will give you a result:这会给你一个结果:

Sales_Date  | Product   | Chanel                 | Sales_Quantity

2017-05-23    Product A   Online_Local_Supplier    400
2018-09-10    Product A   Store_Local_Supplier     200
2018-12-14    Product B   Store_Reseller           600
2019-01-03    Product B   Store_Reseller           700
2019-02-15    Product B   Online_Reseller          650
2019-03-20    Product A   Online_Local_Supplier    380
2019-08-25    Product C   TradeFair_Foreign_Import 120
2019-09-16    Product C   Online_Foreign_Import    470
2019-09-16    Product A   Store_Local_Supplier     920
2019-10-20    Product B   TraidFair_Reseller       860

PS Please do check all the comments jarlh has given you. PS请检查jarlh给你的所有评论。

Here is a DEMO 这是一个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM