左连接SQL后数据不匹配

时间:2017-09-15 08:38:33

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

我的查询正在做的是它使用左连接加入项目,工厂和月份,但我希望我的结果表显示prod和frcast的所有月份,如果找不到月份,则显示null或0。请帮忙。

2 个答案:

答案 0 :(得分: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 |
+---------+-------+-----------+-------+----------+-----------+-------+------+

答案 1 :(得分: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