加入第3个表来提取数据

时间:2017-05-01 09:46:44

标签: mysql sql sql-server excel-vba iseries-navigator

  • 表1 - 保存订单数据 - 表名 - PANFPOS
  • 表2 - 项目主数据 - 表名称 - PBFSTSS
  • 表3 - 保存与表2相关的库存数据 - 表名 - PBESTLO

我有一个从表1和表2中提取数据的查询,在这两个表中有三个常见的字段 - 公司,公司和项目编号。

现在有第三个表格,其中包含与表2中的项目编号关联的批次编号,我需要提取

我目前的查询如下: -

    SELECT HE2DATV6.PANFPOS.APKONZ as Holding, HE2DATV6.PANFPOS.APFIRM as Firm, HE2DATV6.Panfpos.APANR1 as OrderNo, 
HE2DATV6.Panfpos.APIDEN as Item, HE2DATV6.PBFSTSS.TZBEZ1 concat
HE2DATV6.PBFSTSS.TZBEZ2 as Description, HE2DATV6.PBFSTSS.TZBEM1 concat
HE2DATV6.PBFSTSS.TZBEM2 as Family, HE2DATV6.Panfpos.APBSTM as OrderedQty, 
HE2DATV6.Panfpos.APRESM AS AlocatedQty
FROM HE2DATV6.PANFPOS LEFT OUTER JOIN   HE2DATV6.PBFSTSS ON HE2DATV6.PANFPOS.APKONZ = HE2DATV6.PBFSTSS.TZKONZ
    AND HE2DATV6.PANFPOS.APFIRM = HE2DATV6.PBFSTSS.TZFIRM
    AND HE2DATV6.PANFPOS.APIDEN = HE2DATV6.PBFSTSS.TZIDEN 
where he2datv6.panfpos.apanr1 = ? and 
HE2DATV6.Panfpos.APIDEN != 'ENDE'

我写的加入第二和第三个表的查询如下: -

SELECT HE2DATV6.PANFPOS.APKONZ as Holding, HE2DATV6.PANFPOS.APFIRM as Firm, HE2DATV6.Panfpos.APANR1 as OrderNo, 
HE2DATV6.Panfpos.APIDEN as Item, HE2DATV6.PBFSTSS.TZBEZ1 concat
HE2DATV6.PBFSTSS.TZBEZ2 as Description, HE2DATV6.PBFSTSS.TZBEM1 concat
HE2DATV6.PBFSTSS.TZBEM2 as Family, HE2DATV6.PBESTLO.BMPRNR as Batch,
HE2DATV6.Panfpos.APBSTM as OrderedQty, 
HE2DATV6.Panfpos.APRESM AS AlocatedQty
FROM (HE2DATV6.PANFPOS LEFT OUTER JOIN HE2DATV6.PBFSTSS ON HE2DATV6.PANFPOS.APKONZ = HE2DATV6.PBFSTSS.TZKONZ
    AND HE2DATV6.PANFPOS.APFIRM = HE2DATV6.PBFSTSS.TZFIRM
    AND HE2DATV6.PANFPOS.APIDEN = HE2DATV6.PBFSTSS.TZIDEN) 
     (HE2DATV6.PBFSTSS LEFT OUTER JOIN HE2DATV6.PBESTLO ON HE2DATV6.PBFSTSS.TZFIRM = HHE2DATV6.PBFSTSS.BMFIRM 
AND HE2DATV6.PBFSTSS.TZKONZ =  HE2DATV6.PBESTLO.BMKONZ 
AND HE2DATV6.PBFSTSS.TZIDEN =  HE2DATV6.PBESTLO.BMIDEN)
     where he2datv6.panfpos.apanr1 = ? and 
HE2DATV6.Panfpos.APIDEN != 'ENDE'

我在Excel数据连接选项卡上运行此查询以提取报告但我遇到错误SQL01014令牌(无效。

1 个答案:

答案 0 :(得分:0)

您无需再次为另一个LEFT OUTER JOIN指定表名,请尝试以下操作:

SELECT HE2DATV6.PANFPOS.APKONZ as Holding, HE2DATV6.PANFPOS.APFIRM as Firm, HE2DATV6.Panfpos.APANR1 as OrderNo, 
HE2DATV6.Panfpos.APIDEN as Item, HE2DATV6.PBFSTSS.TZBEZ1 concat
HE2DATV6.PBFSTSS.TZBEZ2 as Description, HE2DATV6.PBFSTSS.TZBEM1 concat
HE2DATV6.PBFSTSS.TZBEM2 as Family, HE2DATV6.PBESTLO.BMPRNR as Batch,
HE2DATV6.Panfpos.APBSTM as OrderedQty, 
HE2DATV6.Panfpos.APRESM AS AlocatedQty
FROM HE2DATV6.PANFPOS LEFT OUTER JOIN HE2DATV6.PBFSTSS ON HE2DATV6.PANFPOS.APKONZ = HE2DATV6.PBFSTSS.TZKONZ
    AND HE2DATV6.PANFPOS.APFIRM = HE2DATV6.PBFSTSS.TZFIRM
    AND HE2DATV6.PANFPOS.APIDEN = HE2DATV6.PBFSTSS.TZIDEN
    LEFT OUTER JOIN HE2DATV6.PBESTLO ON HE2DATV6.PBFSTSS.TZFIRM = HHE2DATV6.PBFSTSS.BMFIRM 
    AND HE2DATV6.PBFSTSS.TZKONZ =  HE2DATV6.PBESTLO.BMKONZ 
    AND HE2DATV6.PBFSTSS.TZIDEN =  HE2DATV6.PBESTLO.BMIDEN
where he2datv6.panfpos.apanr1 = ? and 
HE2DATV6.Panfpos.APIDEN != 'ENDE'