在多个表上左联接

时间:2019-01-25 16:08:18

标签: sql ms-access

我正在尝试在MS Access中创建具有两个条件的LEFT JOIN。但是,在下面的代码下运行时,出现“不支持JOIN表达式”。有人知道如何解决吗?问题出在“ d.brand = c.brand”部分。但是我不知道如何重写...

SELECT 
     a.WeekNo, a.EssbaseCustomer, a.SKU, a.SKU_Description, c.[Brand], d.Index, 
     a.Promo_Kg, a.Promo_NSV, b.NonPromo_NSVkg, a.Promo_NSV/a.Promo_kg AS 
     Promo_NSVkg
FROM 
     ((Actuals AS a LEFT JOIN PriceList AS b ON (a.SKU=b.SKU) 
     AND (a.EssbaseCustomer=b.EssbaseCustomer)) LEFT JOIN Map_SKU 
     AS c ON a.SKU=c.SKU) LEFT JOIN Seasonality AS d ON a.Weekno=d.Weekno 
     AND d.brand=c.Brand
WHERE 
     a.Promo_Kg>0;

3 个答案:

答案 0 :(得分:0)

我认为问题出在括号内

SELECT a.WeekNo,
       a.EssbaseCustomer,
       a.SKU,
       a.SKU_Description,
       c.[Brand],
       d.Index,
       a.Promo_Kg,
       a.Promo_NSV,
       b.NonPromo_NSVkg,
       a.Promo_NSV/a.Promo_kg AS Promo_NSVkg
FROM ((Actuals AS a
       LEFT JOIN PriceList AS b ON (a.SKU=b.SKU) AND (a.EssbaseCustomer=b.EssbaseCustomer))
       LEFT JOIN Map_SKU AS c ON a.SKU=c.SKU
       LEFT JOIN Seasonality AS d ON a.Weekno=d.Weekno AND d.brand=c.Brand)
WHERE a.Promo_Kg>0;

或者更好,根本没有括号:

SELECT a.WeekNo,
       a.EssbaseCustomer,
       a.SKU,
       a.SKU_Description,
       c.[Brand],
       d.Index,
       a.Promo_Kg,
       a.Promo_NSV,
       b.NonPromo_NSVkg,
       a.Promo_NSV/a.Promo_kg AS Promo_NSVkg
FROM Actuals AS a
        LEFT JOIN PriceList AS b ON a.SKU=b.SKU AND a.EssbaseCustomer=b.EssbaseCustomer
        LEFT JOIN Map_SKU AS c ON a.SKU=c.SKU
        LEFT JOIN Seasonality AS d ON a.Weekno=d.Weekno AND d.brand=c.Brand
WHERE a.Promo_Kg>0;

希望有帮助...

答案 1 :(得分:0)

瘫痪是多余的。另外,我将where子句移到与之相关的第一个左连接中

SELECT 
     a.WeekNo, a.EssbaseCustomer, a.SKU, a.SKU_Description, c.[Brand], d.Index, 
     a.Promo_Kg, a.Promo_NSV, b.NonPromo_NSVkg, a.Promo_NSV/a.Promo_kg AS 
     Promo_NSVkg
FROM Actuals AS a
LEFT JOIN PriceList AS b 
  ON a.SKU=b.SKU AND a.EssbaseCustomer = b.EssbaseCustomer and a.Promo_Kg>0
LEFT JOIN Map_SKU AS c 
  ON a.SKU=c.SKU 
LEFT JOIN Seasonality AS d 
  ON a.Weekno=d.Weekno 
 AND d.brand=c.Brand

答案 2 :(得分:0)

感谢您与我一起思考!来自Johannes的这段代码不起作用(我必须在括号中加上MS Access的要求):

SELECT a.WeekNo,
   a.EssbaseCustomer,
   a.SKU,
   a.SKU_Description,
   c.[Brand],
   d.Index,
   a.Promo_Kg,
   a.Promo_NSV,
   b.NonPromo_NSVkg,
   a.Promo_NSV/a.Promo_kg AS Promo_NSVkg
FROM (((Actuals AS a
    LEFT JOIN PriceList AS b ON a.SKU=b.SKU AND a.EssbaseCustomer=b.EssbaseCustomer)
    LEFT JOIN Map_SKU AS c ON a.SKU=c.SKU)
    LEFT JOIN Seasonality AS d ON a.Weekno=d.Weekno AND d.brand=c.Brand)
WHERE a.Promo_Kg>0;

但是删除“ d.brand = c.Brand”时,它可以工作。所以出了点问题。