Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two tables in one SQL query and make the date values unique

Tags:

sql

mysql

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;

All this works perfectly.


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?

like image 550
Michi Avatar asked Feb 05 '20 15:02

Michi


People also ask

How can I merge two tables in SQL query?

Multiple tables can be merged by columns in SQL using joins. Joins merge two tables based on the specified columns (generally, the primary key of one table and a foreign key of the other). Below is the generic syntax of SQL joins. USING (id);

What is the most efficient way of joining 2 table in same database?

Method 1: Relational Algebra Relational algebra is the most common way of writing a query and also the most natural way to do so. The code is clean, easy to troubleshoot, and unsurprisingly, it is also the most efficient way to join two tables.

How can I merge two tables in SQL without same columns?

Using the “FROM Table1, Table2” Syntax One way to join two tables without a common column is to use an obsolete syntax for joining tables. With this syntax, we simply list the tables that we want to join in the FROM clause then use a WHERE clause to add joining conditions if necessary.

How can I merge two table data?

You can merge (combine) rows from one table into another simply by pasting the data in the first empty cells below the target table. The table will increase in size to include the new rows.


1 Answers

Use union all and group 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;
like image 69
Gordon Linoff Avatar answered Oct 21 '22 00:10

Gordon Linoff