带有子查询的内连接上的DB2 SQL语法

时间:2018-03-08 15:18:58

标签: sql db2 subquery inner-join

我不是很精通SQL,但我的语法似乎是正确的,我得到“TDEST.ENDZONE在使用它的上下文中无效”,并且到目前为止没有找到关于子查询的内连接的更多信息。

    select * from LYNX.LEGSUM T2 inner join
(    SELECT
      TLORDER.END_ZONE,LEGSUM.LS_DRIVER, LEGSUM.LS_POWER_UNIT
    FROM
      LYNX.LEGSUM LEGSUM
      LEFT OUTER JOIN LYNX.TLORDER TLORDER ON LEGSUM.LS_DLID = TLORDER.DETAIL_LINE_ID
    WHERE
      TLORDER.ORIGIN = 'PRO8060'
      AND LEGSUM.LS_ACTUAL_DATE >= '2017-01-01 00:00:00'
      AND LEGSUM.LS_ACTUAL_DATE < '2018-01-01 00:00:00'
      AND NOT (
        TLORDER.CURRENT_STATUS = 'CANCEL'
        OR TLORDER.CURRENT_STATUS = 'CANCL'
        OR TLORDER.CURRENT_STATUS = 'ENTRY'
      )
) TDEST
ON TDEST.ENDZONE = T2.LEGSUM.LS_FROM_ZONE
AND TDEST.LS_DRIVER = T2.LS_DRIVER
AND TDEST.LS_POWER_UNIT = T2.LS_POWER_UNIT

如果需要任何解释,我想要实现的是从我的LEGSUM表中获取与我的子查询结果相对应的数据集,然后将使用“IN”函数来过滤更多数据

感谢您提供任何帮助

2 个答案:

答案 0 :(得分:1)

重新考虑子查询的使用,因为外部和子查询在单元级别共享相同的源表LYNX.LEGSUM。考虑在同一个表中加入自联接:

SELECT l2.*
FROM
  LYNX.LEGSUM l
LEFT OUTER JOIN LYNX.TLORDER t
  ON l.LS_DLID = t.DETAIL_LINE_ID
INNER JOIN LYNX.LEGSUM l2
  AND l2.LS_FROM_ZONE = t.END_ZONE
  AND l2.LS_DRIVER = l.LS_DRIVER
  AND l2.LS_POWER_UNIT = l.LS_POWER_UNIT

WHERE
  t.ORIGIN = 'PRO8060'
  AND l.LS_ACTUAL_DATE >= '2017-01-01 00:00:00'
  AND l.LS_ACTUAL_DATE < '2018-01-01 00:00:00'
  AND NOT (
    t.CURRENT_STATUS = 'CANCEL'
    OR t.CURRENT_STATUS = 'CANCL'
    OR t.CURRENT_STATUS = 'ENTRY'
  )

答案 1 :(得分:0)

您在ON子句

中使用的END_ZONE中缺少下划线