<legend id='dwrGS'><style id='dwrGS'><dir id='dwrGS'><q id='dwrGS'></q></dir></style></legend>
    1. <tfoot id='dwrGS'></tfoot>

        <bdo id='dwrGS'></bdo><ul id='dwrGS'></ul>

      <small id='dwrGS'></small><noframes id='dwrGS'>

      <i id='dwrGS'><tr id='dwrGS'><dt id='dwrGS'><q id='dwrGS'><span id='dwrGS'><b id='dwrGS'><form id='dwrGS'><ins id='dwrGS'></ins><ul id='dwrGS'></ul><sub id='dwrGS'></sub></form><legend id='dwrGS'></legend><bdo id='dwrGS'><pre id='dwrGS'><center id='dwrGS'></center></pre></bdo></b><th id='dwrGS'></th></span></q></dt></tr></i><div id='dwrGS'><tfoot id='dwrGS'></tfoot><dl id='dwrGS'><fieldset id='dwrGS'></fieldset></dl></div>

        在一个 SQL 查询中合并两个表并使日期值唯一

        时间:2023-05-24

          1. <tfoot id='pZclQ'></tfoot>

              • <small id='pZclQ'></small><noframes id='pZclQ'>

                  <bdo id='pZclQ'></bdo><ul id='pZclQ'></ul>
                • <legend id='pZclQ'><style id='pZclQ'><dir id='pZclQ'><q id='pZclQ'></q></dir></style></legend>
                    <tbody id='pZclQ'></tbody>
                  <i id='pZclQ'><tr id='pZclQ'><dt id='pZclQ'><q id='pZclQ'><span id='pZclQ'><b id='pZclQ'><form id='pZclQ'><ins id='pZclQ'></ins><ul id='pZclQ'></ul><sub id='pZclQ'></sub></form><legend id='pZclQ'></legend><bdo id='pZclQ'><pre id='pZclQ'><center id='pZclQ'></center></pre></bdo></b><th id='pZclQ'></th></span></q></dt></tr></i><div id='pZclQ'><tfoot id='pZclQ'></tfoot><dl id='pZclQ'><fieldset id='pZclQ'></fieldset></dl></div>

                  本文介绍了在一个 SQL 查询中合并两个表并使日期值唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有以下两个表,你也可以在 SQL fiddle 这里找到:

                  I have the following two tables which you can also find in the 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");
                  

                  我使用 此处 合并两个表:

                  I use the VBA from here to merge the two tables:

                  (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;
                  

                  所有这些都完美无缺.

                  但是,现在我希望日期显示为唯一的.
                  结果应该是这样的:

                  However, now I want that the dates are displayed unique.
                  The result should look like this:

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

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

                  What do I need to change in my code to make it work?

                  推荐答案

                  使用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;
                  

                  这篇关于在一个 SQL 查询中合并两个表并使日期值唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用“like"优化 MySQL 搜索和通配符 下一篇:自动增量主要在计数中留下空白

                  相关文章

                  <i id='N4IVI'><tr id='N4IVI'><dt id='N4IVI'><q id='N4IVI'><span id='N4IVI'><b id='N4IVI'><form id='N4IVI'><ins id='N4IVI'></ins><ul id='N4IVI'></ul><sub id='N4IVI'></sub></form><legend id='N4IVI'></legend><bdo id='N4IVI'><pre id='N4IVI'><center id='N4IVI'></center></pre></bdo></b><th id='N4IVI'></th></span></q></dt></tr></i><div id='N4IVI'><tfoot id='N4IVI'></tfoot><dl id='N4IVI'><fieldset id='N4IVI'></fieldset></dl></div>
                      <legend id='N4IVI'><style id='N4IVI'><dir id='N4IVI'><q id='N4IVI'></q></dir></style></legend>
                    1. <tfoot id='N4IVI'></tfoot>

                      • <bdo id='N4IVI'></bdo><ul id='N4IVI'></ul>

                    2. <small id='N4IVI'></small><noframes id='N4IVI'>