首先,我不太熟悉SQL。
我试图根据子查询的记录过滤结果:
SELECT DISTCINT
t.end_zone, l2.LS_TRIP_NUMBER, l2.LS_DRIVER,
l2.LS_POWER_UNIT, l2.LS_ACTUAL_DATE
FROM
LYNX.LEGSUM l
LEFT OUTER JOIN
LYNX.TLORDER t ON l.LS_DLID = t.DETAIL_LINE_ID
INNER JOIN
LYNX.LEGSUM l2 ON 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')
现在,我想要做的是根据以下内容从我的Legsum(LS)表中过滤:
-LS.Origin = t.endzone
-LS.Power_unit = l2.power_unit
-LS.Driver = l2.Driver
-LS.actual_date Between l.actual_date and DAY(l.actual_date)+1
我遇到的问题是,当我尝试在子查询的结果上获得所有这些条件时,这是我尝试过的:
SELECT
LEGSUM.LS_TRIP_NUMBER, LEGSUM.LS_LEG_DIST, LS_MT_LOADED
FROM
LYNX.LEGSUM LEGSUM
LEFT OUTER JOIN
LYNX.TLORDER TLORDER ON LEGSUM.LS_DLID = TLORDER.DETAIL_LINE_ID
WHERE
LEGSUM.LS_ACTUAL_DATE >= '2017-01-01 00:00:00'
AND LEGSUM.LS_ACTUAL_DATE < '2018-01-01 00:00:00'
AND NOT (TLORDER.BILL_NUMBER LIKE 'A%'
OR TLORDER.BILL_NUMBER LIKE 'C%'
OR TLORDER.BILL_NUMBER LIKE 'L%'
OR TLORDER.BILL_NUMBER LIKE 'Q%'
OR TLORDER.BILL_NUMBER LIKE 'MA%')
AND NOT (TLORDER.CURRENT_STATUS = 'CANCEL'
OR TLORDER.CURRENT_STATUS = 'CANCL'
OR TLORDER.CURRENT_STATUS = 'ENTRY')
AND LEGSUM.LS_FROM_ZONE IN (SELECT DISTINCT t.end_zone, l2.LS_TRIP_NUMBER, l2.LS_DRIVER, l2.LS_POWER_UNIT
FROM LYNX.LEGSUM l
LEFT OUTER JOIN LYNX.TLORDER t ON l.LS_DLID = t.DETAIL_LINE_ID
INNER JOIN LYNX.LEGSUM l2 ON 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')
)
这是第一个条件而不是其他3
感谢您提供任何帮助
答案 0 :(得分:0)
考虑EXISTS
而不是IN
,其中您将子查询列与外部查询列相关联。请参阅子查询中的最后4个WHERE
子句条件,以根据需要进行修改。此方法建议在外部查询中使用表别名来进行相关。
SELECT
l.LS_TRIP_NUMBER, l.LS_LEG_DIST, l.LS_MT_LOADED
FROM
LYNX.LEGSUM l
LEFT OUTER JOIN
LYNX.TLORDER t ON l.LS_DLID = t.DETAIL_LINE_ID
WHERE
l.LS_ACTUAL_DATE >= '2017-01-01 00:00:00'
AND l.LS_ACTUAL_DATE < '2018-01-01 00:00:00'
AND NOT (t.BILL_NUMBER LIKE 'A%' OR t.BILL_NUMBER LIKE 'C%'
OR t.BILL_NUMBER LIKE 'L%' OR t.BILL_NUMBER LIKE 'Q%'
OR t.BILL_NUMBER LIKE 'MA%')
AND NOT (t.CURRENT_STATUS = 'CANCEL'
OR t.CURRENT_STATUS = 'CANCL'
OR t.CURRENT_STATUS = 'ENTRY')
AND EXISTS (SELECT 1
FROM LYNX.LEGSUM sub_l
LEFT OUTER JOIN LYNX.TLORDER sub_t ON sub_l.LS_DLID = sub_t.DETAIL_LINE_ID
INNER JOIN LYNX.LEGSUM sub_l2 ON sub_l2.LS_FROM_ZONE = t.END_ZONE
AND sub_l2.LS_DRIVER = sub_l.LS_DRIVER
AND sub_l2.LS_POWER_UNIT = sub_l.LS_POWER_UNIT
WHERE sub_t.ORIGIN = 'PRO8060'
AND sub_l.LS_ACTUAL_DATE >= '2017-01-01 00:00:00'
AND sub_l.LS_ACTUAL_DATE < '2018-01-01 00:00:00'
AND NOT (sub_t.CURRENT_STATUS = 'CANCEL'
OR sub_t.CURRENT_STATUS = 'CANCL'
OR sub_t.CURRENT_STATUS = 'ENTRY')
AND l.LS_FROM_ZONE = sub_t.endzone
AND l.LS_POWER_UNIT = sub_l2.LS_POWER_UNIT
AND l.LS_DRIVER = sub_l2.LS_DRIVER
AND l.LS_ACTUAL_DATE BETWEEN sub_l.LS_ACTUAL_DATE
AND (sub_l.LS_ACTUAL_DATE + 1 DAYS)
)