【问题标题】:Merge two tables in one SQL query and make the date values unique在一个 SQL 查询中合并两个表并使日期值唯一
【发布时间】:2020-02-05 15:03:00
【问题描述】:

我有以下两个表,您也可以在 SQL fiddle here 中找到它们:

CREATE TABLE Inbound (
    Inbound_Date DATE,
    Product TEXT,
    InboundType TEXT,
    Quantity VARCHAR(255)
);

INSERT INTO Inbound
(Inbound_Date, Product, InboundType, Quantity)
VALUES 
("2017-05-23", "Product A", "Supplier", "400"),
("2018-09-10", "Product B", "Supplier", "200"),
("2018-12-14", "Product B", "Supplier", "600"),
("2019-01-03", "Product A", "Return", "700"),
("2019-02-15", "Product C", "Supplier", "650"),
("2017-09-04", "Product C", "Supplier", "380"),
("2019-01-09", "Product A", "Return", "120"),
("2019-02-16", "Product A", "Return", "470"),
("2019-02-12", "Product A", "Supplier", "920"),
("2019-02-15", "Product C", "Return", "860"),
("2018-01-03", "Product B", "Supplier", "610");


CREATE TABLE Outbound (
    Outbound_Date DATE,
    Product TEXT,
    OutboundType TEXT
);

INSERT INTO Outbound
(Outbound_Date, Product, OutboundType)
VALUES 
("2017-05-23", "Product A", "Sale_US"),
("2018-09-10", "Product B", "Sale_DE"),
("2018-12-18", "Product B", "Sale_DE"),
("2019-02-01", "Product A", "Sale_DE"),
("2019-02-22", "Product C", "Sale_FR"),
("2017-10-18", "Product C", "Sale_NL"),
("2019-04-12", "Product A", "Sale_US"),
("2019-04-12", "Product A", "Sale_FR"),
("2019-04-12", "Product A", "Sale_FR"),
("2019-04-19", "Product C", "Sale_US"),
("2018-05-17", "Product B", "Sale_DE");

我使用来自here 的 VBA 来合并两个表:

(SELECT 
   Inbound_Date As Date, 
   Product, 
   SUM(Quantity) as Inbound, 0 as Outbound
 FROM Inbound
 GROUP BY 1,2
) 

UNION ALL

(SELECT
   Outbound_Date,
   Product,
   0 as Inbound, COUNT("Outbound_Type")  as Outbound 
 FROM Outbound
 GROUP BY 1,2
)

ORDER BY 1,2;

这一切都很完美。


但是,现在我希望日期显示是唯一的。
结果应如下所示:

Date           Product       Inbound        Outbound
2017-05-13     Product A     400            1
2017-09-04     Product C     380            0
2017-10-18     Product C      0             1
:              :             :              :
:              :             :              :
2018-09-10     Product B     200            1
:              :             :              :
:              :             :              :

我需要对我的代码进行哪些更改才能使其正常工作?

【问题讨论】:

  • 是的。这就是提问的方式。
  • 这看起来很像来自your previous question 的副本,我的回答实际上解决了您在此处描述的问题(与您在此问题中所指的已接受答案不同)。

标签: mysql sql


【解决方案1】:

使用union allgroup by

SELECT Date, Product, SUM(Inbound) as Inbound, SUM(Outbound) as Outbound
FROM ((SELECT Inbound_Date As Date, Product, SUM(Quantity) as Inbound, 0 as Outbound
      FROM Inbound
      GROUP BY 1,2
     ) UNION ALL
     (SELECT Outbound_Date, Product, 0 as Inbound, COUNT(*)  as Outbound 
      FROM Outbound
      GROUP BY 1,2
     )
    ) io
GROUP BY Date, Product;

【讨论】:

    【解决方案2】:

    您可以尝试以下查询。先收集所有行并聚合行(SQL FIDDLE):

    SELECT IO.Date, IO.Product, SUM(IO.Inbound) as Inbound, SUM(IO.Outbound) as Outbound
    FROM (
          SELECT Inbound_Date As Date, Product, Quantity as Inbound, 0 as Outbound
          FROM Inbound    
          UNION ALL  
          SELECT Outbound_Date as Date, Product, 0 as Inbound, 1  as Outbound 
          FROM Outbound   
        ) as IO
    GROUP BY IO.Date, IO.Product;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2019-04-03
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      相关资源
      最近更新 更多