2017-09-15 65 views
-2

我有兩個表,一個包含生產數據,另一個包含預測數據。我加入這兩張表格,將實際生產數據與預測數據進行比較。 我的示例表如下:左連接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 

查詢用來連接:

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

結果: 加入

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 

過濾特定一年後更好地解釋問題

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 

所需的表

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 

什麼我的查詢做的是,它使用LEFT JOIN聯接項目,廠房及一個月,但我想我得到的表格中顯示兩個督促所有月份和frcast,如果找不到月份,則顯示null或零。請幫忙。

+0

使用FULL加入而不是左方加入 –

+0

您正在使用哪個[DBMS](https://en.wikipedia.org/wiki/DBMS)產品? Postgres的?甲骨文? –

+0

看起來像Mysql –

回答

0

你可以試試這個。 FULL JOIN之後的子查詢是從Products表中僅提取一年。 我也爲ORDER BY添加了一個CASE。

一年版

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 ; 

請注意,您的樣本數據(第一張表)是不完整的。

輸出:

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

新增:多年的版本,與組通過對PROD表

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 ; 

輸出:當你

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

上述查詢不起作用,因爲我的預測表只包含前瞻性數據,因此不會包含與我的prod表不同的前幾年。 – Sqlnewbee

+0

@sqlnewbee我正在看看... – etsa

+0

@Sqlnewbee更新了答案 – etsa

0

使用FULL OUTER JOIN過濾特定年份的記錄

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

我編輯查詢作爲完整的外連接,但它產生與左連接相同的結果。有沒有什麼辦法可以創建一個交叉連接,每個Fyear映射每個星期,每個Fmonth映射每個星期,然後執行一個左連接。 – Sqlnewbee