繁体   English   中英

在一个 SQL 查询中合并两个表中的两列,并使用表中的值作为列名

[英]Merge two columns from two tables in one SQL query and use value in tables as column name

我有以下两个表,您也可以在此处SQL fiddle找到

CREATE TABLE Send_Orders (
    Send_Date DATE,
    Product TEXT,
    FlowType TEXT,
    Send_Quantity VARCHAR(255)
);

INSERT INTO Send_Orders
(Send_Date, Product, FlowType, Send_Quantity)
VALUES 
("2017-05-23", "Product A", "Send", "400"),
("2018-09-10", "Product B", "Send", "200"),
("2018-12-14", "Product B", "Send", "600"),
("2019-01-03", "Product A", "Send", "700"),
("2019-02-15", "Product C", "Send", "650"),
("2017-09-04", "Product C", "Send", "380"),
("2019-01-09", "Product A", "Send", "120"),
("2019-02-16", "Product A", "Send", "470"),
("2019-02-12", "Product A", "Send", "920"),
("2019-02-15", "Product C", "Send", "860"),
("2018-01-03", "Product B", "Send", "610");


CREATE TABLE Return_Orders (
    Return_Date DATE,
    Product TEXT,
    DeliveryType TEXT
);

INSERT INTO Return_Orders
(Return_Date, Product, DeliveryType)
VALUES 
("2017-06-24", "Product A", "Return"),
("2018-12-18", "Product B", "Return"),
("2018-12-18", "Product B", "Return"),
("2019-02-01", "Product A", "Return"),
("2019-02-22", "Product C", "Return"),
("2017-10-18", "Product C", "Return"),
("2019-04-12", "Product A", "Return"),
("2019-04-12", "Product A", "Return"),
("2019-04-12", "Product A", "Return"),
("2019-04-19", "Product C", "Return"),
("2018-05-17", "Product B", "Return");

我使用以下 SQL 合并两个表:

(SELECT Send_Date As Event_Date, Product, FlowType, 
       SUM(Send_Quantity) as Quantity
 FROM Send_Orders
 GROUP BY Send_Date, Product, FlowType
) 
UNION ALL
(SELECT Return_Date, Product, DeliveryType, COUNT("Product") 
 FROM Return_Orders
 GROUP BY Return_Date, Product, DeliveryType
)
ORDER BY 1,2;

所有这些工作正常。


现在,我想实现FlowTypeDeliveryType中的值用作columnname
最后查询结果应该是这样的:

Event-Date      Product      Send_Quantity      Return_Quantity   
2017-05-23     Product A       400                NULL
2017-06-24     Product A       NULL               1      
2017-09-04     Product C       380                NULL
2017-10-18     Product C       NULL               1
:              :               :                  :
:              :               :                  :
:              :               :                  :

我需要在我的 SQL 代码中更改什么才能使其工作?

使用union allgroup by

SELECT Event_date, Product, SUM(send_quantity), SUM(return_quantity)
FROM ((SELECT Send_Date As Event_Date, Product, FlowType, 
              SUM(Send_Quantity) as send_quantity,
              0 as return_quantity
       FROM Send_Orders
       GROUP BY Send_Date, Product, FlowType
      ) UNION ALL
      (SELECT Return_Date, Product, DeliveryType, 
              0 as send_quantity,
              COUNT(*) as return_quantity
       FROM Return_Orders
       GROUP BY Return_Date, Product, DeliveryType
      )
     ) sr
GROUP BY Event_date, Product;

您可以从子查询中删除FlowTypeDeliveryType 但是您的查询包含它们,因此出于某种原因它们可能有用。

暂无
暂无

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

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