Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Führen Sie zwei Tabellen in einer SQL-Abfrage zusammen und machen Sie die Datumswerte eindeutig

Verwenden Sie union all und 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;