簡體   English   中英

連接兩個表並合並一列中的文本值

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

我有以下兩個表,您也可以在此處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");

注意:每個產品都明確分配到一個購買渠道!


現在,我想做一個查詢,它給我以下結果:

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
:            :            :                         :
:            :            :                         :
:            :            :                         :

如您所見,我想將Sales_ChannelPurchasing_Channel合並在一列中。
因此,我設置了以下查詢:

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;

此查詢正確插入Sales_Channel但我必須如何更改它以便將Purchasing_Channel添加到其中,如我想要的結果所示?

嘗試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

嘗試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

這會給你一個結果:

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請檢查jarlh給你的所有評論。

這是一個演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM