2017-09-15 2 views
-2

Ich habe zwei Tabellen, eine, die Produktionsdaten enthält und die anderen Daten prognostiziert hat. Ich schließe mich den beiden Tabellen an, um die tatsächlichen Produktionsdaten mit den prognostizierten Daten zu vergleichen. Meine Beispieltabellen sind wie folgt:Datenkonflikt nach linker Verbindung SQL

**Prod Tbl**     
Product Plant pmonth pyear quantity 
B007 2  January 2014 45 
B007 2  February 2014 270 
B007 2  March  2014 270 
B007 2  April  2014 45 
B007 2  May  2014 90 
B007 2  May  2014 90 
B007 2  June  2014 90 
B007 2  June  2014 90 
B007 2  July  2014 135 
B007 2  July  2014 45 
B007 2  August 2014 135 
B007 2  August 2014 135 
B007 2  July  2015 90 
B007 2  August 2014 135 
B007 2  September 2014 135 
B007 2  September 2015 135 
B007 2  October 2015 90 
B007 2  September 2014 135 
B007 2  September 2014 90 
B007 2  September 2014 90 
B007 2  November 2014 254 
B007 2  May  2016 90 
B007 2  August 2016 135 
B007 2  October 2016 87 

**Forecast Tbl**     
Product Plant Fmonth Fyear Fqty 
B007 2  July  2017 100 
B007 2  August 2017 100 
B007 2  September 2017 100 
B007 2  October 2017 100 
B007 2  Novenmber 2017 100 
B007 2  December 2017 100 

Abfrage verwendet, um Mitglied werden:

Select a.Product, 
     a.plant, 
     b.pmonth, 
     b.pyear, 
     coalesce(b.quantity,0) as quantity, 
     a.fmonth, 
     a.fyear,coalesce(a.fqty,0) as fqty 
from 
Frcast_Tbl as a 
    left join on Prod_Tbl as b on (a.Product = b.Product 
          and a.Plant = b.plant 
          and b.pMonth = a.fMonth); 

Ergebnis: Nach seinem Eintritt in

Product Plant Pmonth Pyear Quantity Fmonth Fyear fqty 
B007 2  July  2014 180   July  2017 100 
B007 2  July  2015 90   July  2017 100 
B007 2  August 2014 405   August 2017 100 
B007 2  August 2016 315   August 2017 100 
B007 2  September 2014 450   September 2017 100 
B007 2  September 2015 135   September 2017 100 
B007 2  October 2016 177   October 2017 100 
B007 2  October 2015 90   October 2017 100 
B007 2  November 2014 356   November 2017 100 
B007 2  December 2016 90   December 2017 100 
B007 2  January 2015 90   January 2018 100 
B007 2  January 2016 90   January 2018 100 
B007 2  January 2014 45   January 2018 100 
B007 2  January 2017 90   January 2018 100 
B007 2  February 2014 270   February 2018 99 
B007 2  March  2014 270   March  2018 101 
B007 2  March  2017 90   March  2018 101 
B007 2  April  2014 45   April  2018 100 
B007 2  May  2016 90   May  2018 100 
B007 2  May  2014 180   May  2018 100 
B007 2  May  2017 90   May  2018 100 

für ein bestimmtes Jahr zu filternde besser das Problem erklären

Producr plant pmonth pyear quantity fmonth fyear fqty 
B007 2  August 2016 315   August 2017 100 
B007 2  October 2016 177   October 2017 100 
B007 2  December 2016 90   December 2017 100 

Gewünschte Tabelle

Product Plant Pmonth Pyear Quantity fmonth  fyear fqty 
B007 2  January 2016 90   null  null  0 
B007 2  May  2016 90   null  null  0 
B007 2  June  2016 270   null  null  0 
B007 2  null  null 0   July  2017 100 
B007 2  August 2016 315   August  2017 100 
B007 2  null  null 0   September 2017 100 
B007 2  October 2016 177   October 2017 100 
B007 2  null  null 0   November 2017 100 
B007 2  December 2016 90   December 2017 100 

Was meine Frage macht, ist, dass es schließt sich Artikel, Pflanze und Monat linken join aber ich möchte, dass meine resultierende Tabelle für beide prod alle Monate anzuzeigen und frcast und in Fällen, in denen der Monat nicht gefunden wird, Null oder Nullen anzeigen. Bitte helfen Sie.

+0

verwenden FULL Join statt LEFT join –

+0

Welches [DBMS] (https://en.wikipedia.org/wiki/DBMS) Produkt verwenden Sie? Postgres? Orakel? –

+0

Scheint wie Mysql –

Antwort

0

Sie könnten dies versuchen. Die Unterabfrage nach FULL JOIN dient dazu, nur ein Jahr aus der Products-Tabelle zu extrahieren. Ich habe einen CASE für ORDER BY hinzugefügt.

Ein Jahr Version

SELECT COALESCE(a.Product,b.Product) AS PRODUCT, 
     COALESCE(a.plant,b.plant) AS PLANT, 
     b.pmonth, 
     b.pyear, 
     coalesce(b.quantity,0) as quantity, 
     a.fmonth AS FMONTH, 
     a.fyear, 
     coalesce(a.fqty,0) as fqty 
FROM FORECAST A 
FULL JOIN (SELECT * FROM PROD WHERE pyear=2016) B on a.Product = b.Product 
                 and a.Plant = b.plant 
                 and A.fmonth = b.pMonth 
ORDER BY CASE COALESCE(b.pmonth, a.fmonth) 
      WHEN 'January' THEN 1 
      WHEN 'February' THEN 2 
      WHEN 'March' THEN 3 
      WHEN 'April' THEN 4 
      WHEN 'May' THEN 5 
      WHEN 'June' THEN 6 
      WHEN 'July' THEN 7 
      WHEN 'August' THEN 8 
      WHEN 'September' THEN 9 
      WHEN 'October' THEN 10 
      WHEN 'November' THEN 11 
      WHEN 'December' THEN 12 
      END ; 

Pls beachten Sie, dass Ihre Probendaten (erste Tabelle) nicht vollständig sind.

Ausgang:

+-------------+-------+---------+-------+----------+-----------+-------+------+ 
|  PRODUCT | PLANT | pmonth | pyear | quantity | FMONTH | fyear | fqty | 
+-------------+-------+---------+-------+----------+-----------+-------+------+ 
|  B007 |  2 | January | 2016 |  90 | NULL  | NULL | 0 | 
|  B007 |  2 | May  | 2016 |  90 | NULL  | NULL | 0 | 
|  B007 |  2 | June | 2016 |  270 | NULL  | NULL | 0 | 
|  B007 |  2 | NULL | NULL |  0 | July  | 2017 | 100 | 
|  B007 |  2 | August | 2016 |  135 | August | 2017 | 100 | 
|  B007 |  2 | NULL | NULL |  0 | September | 2017 | 100 | 
|  B007 |  2 | October | 2016 |  87 | October | 2017 | 100 | 
|  B007 |  2 | NULL | NULL |  0 | November | 2017 | 100 | 
|  B007 |  2 | NULL | NULL |  0 | December | 2017 | 100 | 
+-------------+-------+---------+-------+----------+-----------+-------+------+ 

Hinzugefügt: Mehrjahresvariante, mit der Gruppe von auf ART Tabelle

SELECT COALESCE(a.Product,b.Product) AS PRODUCT, 
     COALESCE(a.plant,b.plant) AS PLANT, 
     b.pmonth, 
     COALESCE(b.pyear,Y.pyear) AS pyear, 
     COALESCE(b.quantity,0) as quantity, 
     a.fmonth AS FMONTH, 
     a.fyear, 
     coalesce(a.fqty,0) as fqty 
FROM FORECAST A 
CROSS JOIN (SELECT DISTINCT pyear FROM PROD /* WHERE pyear IN (2015,2016)*/) Y 
FULL JOIN (SELECT Product, Plant, pyear, pmonth, SUM(quantity) AS quantity 
      FROM PROD /*WHERE pyear IN (2015,2016)*/ 
      GROUP BY Product, Plant, pyear, pmonth 
      ) B on a.Product = b.Product 
        and a.Plant = b.plant 
        and A.fmonth = b.pMonth 
        AND Y.pyear= B.pyear 
ORDER BY COALESCE(b.pyear,Y.pyear), CASE COALESCE(b.pmonth, a.fmonth) 
      WHEN 'January' THEN 1 
      WHEN 'February' THEN 2 
      WHEN 'March' THEN 3 
      WHEN 'April' THEN 4 
      WHEN 'May' THEN 5 
      WHEN 'June' THEN 6 
      WHEN 'July' THEN 7 
      WHEN 'August' THEN 8 
      WHEN 'September' THEN 9 
      WHEN 'October' THEN 10 
      WHEN 'November' THEN 11 
      WHEN 'December' THEN 12 
      END ; 

Ausgang:

+---------+-------+-----------+-------+----------+-----------+-------+------+ 
| PRODUCT | PLANT | pmonth | pyear | quantity | FMONTH | fyear | fqty | 
+---------+-------+-----------+-------+----------+-----------+-------+------+ 
| B007 |  2 | January | 2014 |  45 | NULL  | NULL | 0 | 
| B007 |  2 | February | 2014 |  270 | NULL  | NULL | 0 | 
| B007 |  2 | March  | 2014 |  270 | NULL  | NULL | 0 | 
| B007 |  2 | April  | 2014 |  45 | NULL  | NULL | 0 | 
| B007 |  2 | May  | 2014 |  180 | NULL  | NULL | 0 | 
| B007 |  2 | June  | 2014 |  180 | NULL  | NULL | 0 | 
| B007 |  2 | July  | 2014 |  180 | July  | 2017 | 100 | 
| B007 |  2 | August | 2014 |  405 | August | 2017 | 100 | 
| B007 |  2 | September | 2014 |  450 | September | 2017 | 100 | 
| B007 |  2 | NULL  | 2014 |  0 | October | 2017 | 100 | 
| B007 |  2 | November | 2014 |  254 | November | 2017 | 100 | 
| B007 |  2 | NULL  | 2014 |  0 | December | 2017 | 100 | 
| B007 |  2 | July  | 2015 |  90 | July  | 2017 | 100 | 
| B007 |  2 | NULL  | 2015 |  0 | August | 2017 | 100 | 
| B007 |  2 | September | 2015 |  135 | September | 2017 | 100 | 
| B007 |  2 | October | 2015 |  90 | October | 2017 | 100 | 
| B007 |  2 | NULL  | 2015 |  0 | November | 2017 | 100 | 
| B007 |  2 | NULL  | 2015 |  0 | December | 2017 | 100 | 
| B007 |  2 | January | 2016 |  90 | NULL  | NULL | 0 | 
| B007 |  2 | May  | 2016 |  90 | NULL  | NULL | 0 | 
| B007 |  2 | June  | 2016 |  270 | NULL  | NULL | 0 | 
| B007 |  2 | NULL  | 2016 |  0 | July  | 2017 | 100 | 
| B007 |  2 | August | 2016 |  135 | August | 2017 | 100 | 
| B007 |  2 | NULL  | 2016 |  0 | September | 2017 | 100 | 
| B007 |  2 | October | 2016 |  87 | October | 2017 | 100 | 
| B007 |  2 | NULL  | 2016 |  0 | November | 2017 | 100 | 
| B007 |  2 | NULL  | 2016 |  0 | December | 2017 | 100 | 
+---------+-------+-----------+-------+----------+-----------+-------+------+ 
+0

Die obige Abfrage funktioniert nicht, da meine Vorhersagetabelle nur vorwärts schauende Daten enthält und daher keine früheren Jahre im Gegensatz zu meiner prod Tabelle enthält. – Sqlnewbee

+0

@sqlnewbee Ich gebe einen Blick ... – etsa

+0

@Sqlnewbee aktualisierte Antwort – etsa

0

Verwenden FULL OUTER JOIN, wenn Sie sind Filtern von Datensätzen für ein bestimmtes Jahr

Select a.Product,a.plant,b.pmonth,b.pyear,coalesce(b.quantity,0) as quantity,a.fmonth,a.fyear,coalesce(a.fqty,0) as fqty 
from Frcast_Tbl as a 
FULL OUTER JOIN on Prod_Tbl as b on a.Product = b.Product and 
            a.Plant = b.plant and 
            b.pMonth = a.fMonth 
+0

Ich habe die Abfrage als vollständigen äußeren Join bearbeitet, aber es liefert das gleiche Ergebnis wie linker Join. Gibt es eine Möglichkeit, einen Cross Join zu erstellen, bei dem jedes Jahr mit jedem Jahr, jedes Monat mit jedem Monat mappt und dann einen Linksbündnis macht. – Sqlnewbee