我遇到了正確過濾我的表F0005的問題。我嘗試過許多不同方式的查詢。我想要完成的是在加入之前過濾F0005。LEFT JOIN嵌套Select語句
這就是我想如何過濾,結果是完美的表。
SELECT LTRIM(F0005.DRKY), F0005.DRDL01, F0005.DRRT, F0005.DRSY
FROM
SENCOM.F0005 F0005
WHERE LTRIM(F0005.DRKY) != '' AND F0005.DRRT IN ('W1','08') AND F0005.DRSY NOT IN ('30','32','98')
這是我的整個查詢和我在嘗試過濾表之前加入它。
SELECT
FSALES2011.SXAN8,
FSALES2011.SXCO AS Company,
FSALES2011.SXMCU AS BuisinessUnit,
FSALES2011.SXLITM AS ItemNumber,
FSALES2011.SXSLSM AS SalesPersonCode,
FSALES2011.SXDCTO AS OrderType,
FSALES2011.SXSLD1 AS SoldTo,
FSALES2011.SXADD1 AS Address,
FSALES2011.SXRP01 AS Division,
FSALES2011.SXRP02 AS Location,
FSALES2011.SXCI13+FSALES2011.SXCI14+FSALES2011.SXCI15+FSALES2011.SXCI16+FSALES2011.SXCI17+
FSALES2011.SXCI18+FSALES2011.SXCI19+FSALES2011.SXCI20+FSALES2011.SXCI21+FSALES2011.SXCI22+
FSALES2011.SXCI23+FSALES2011.SXCI24 AS PurchasePrice,
FSALES2011.SXAS13+FSALES2011.SXAS14+FSALES2011.SXAS15+FSALES2011.SXAS16+FSALES2011.SXAS17+
FSALES2011.SXAS18+FSALES2011.SXAS19+FSALES2011.SXAS20+FSALES2011.SXAS21+FSALES2011.SXAS22+
FSALES2011.SXAS23+FSALES2011.SXAS24 AS SalesPrice,
F4801.WAAN8,
F4801.WAWR01 AS WoType,
DIGITS(F4801.WADOCO) AS F4801ItemNumber,
F0101.ABAN8,
F0101.ABAC15 AS F0101CustomerType,
F0006.MCRP08 AS JobType,
SUBSTR(F0006.MCMCU,6,7) AS F0006BuisnessUnit,
LTRIM(F0005Filtered.DRKY) AS UDC,
F0005Filtered.DRDL01 AS Description,
F0005Filtered.DRRT,
F0005Filtered.DRSY
FROM
SENDTA.F0101 F0101
JOIN
JDEMOD.FSALES2011 FSALES2011
ON
FSALES2011.SXAN8 = F0101.ABAN8
LEFT OUTER JOIN
SENDTA.F0006 F0006
ON
FSALES2011.SXLITM = SUBSTR(F0006.MCMCU,6,7)
LEFT OUTER JOIN
SENDTA.F4801 F4801
ON
FSALES2011.SXLITM = DIGITS(F4801.WADOCO)
LEFT OUTER JOIN
(
SELECT
LTRIM(F0005.DRKY),
F0005.DRDL01,
F0005.DRRT,
F0005.DRSY
FROM
SENCOM.F0005 F0005
WHERE
LTRIM(F0005.DRKY) != ''
AND F0005.DRRT IN ('W1',
'08')
AND F0005.DRSY NOT IN ('30',
'32',
'98')) F0005Filtered
ON
ABAC15 = F0005Filtered.DRKY
當我嘗試這個我獲得列錯誤,SQL0205]列DRKY不表F0005FILTERED在* N。
感謝您的任何幫助,我對此有所瞭解,並且沒有想法! :)
巨大的感謝大家,我的問題是我的嵌套選擇的格式,但也完全不同。我加入了錯誤的領域,所以沒有匹配,這就是爲什麼我得到NULL!再次感謝!!
這是最終版本。我打開一些優化建議。否則很高興完成!
SELECT
FSALES2011.SXAN8,
FSALES2011.SXCO AS Company,
FSALES2011.SXMCU AS BuisinessUnit,
FSALES2011.SXLITM AS ItemNumber,
FSALES2011.SXSLSM AS SalesPersonCode,
FSALES2011.SXDCTO AS OrderType,
FSALES2011.SXSLD1 AS SoldTo,
FSALES2011.SXADD1 AS Address,
FSALES2011.SXRP01 AS Division,
FSALES2011.SXRP02 AS Location,
FSALES2011.SXCI13+FSALES2011.SXCI14+FSALES2011.SXCI15+FSALES2011.SXCI16+FSALES2011.SXCI17+
FSALES2011.SXCI18+FSALES2011.SXCI19+FSALES2011.SXCI20+FSALES2011.SXCI21+FSALES2011.SXCI22+
FSALES2011.SXCI23+FSALES2011.SXCI24 AS PurchasePrice,
FSALES2011.SXAS13+FSALES2011.SXAS14+FSALES2011.SXAS15+FSALES2011.SXAS16+FSALES2011.SXAS17+
FSALES2011.SXAS18+FSALES2011.SXAS19+FSALES2011.SXAS20+FSALES2011.SXAS21+FSALES2011.SXAS22+
FSALES2011.SXAS23+FSALES2011.SXAS24 AS SalesPrice,
F4801.WAAN8,
F4801.WAWR01 AS WoType,
DIGITS(F4801.WADOCO) AS F4801ItemNumber,
F0101.ABAN8,
F0101.ABAC15 AS F0101CustomerType,
F0006.MCRP08 AS JobType,
SUBSTR(F0006.MCMCU,6,7) AS F0006BuisnessUnit,
LTRIM(UDC.DRKY) AS Code,
--UDC.DRDL01 AS JobDescription,
--UDC2.DRDL01 AS WODescription,
COALESCE(UDC.DRDL01,UDC2.DRDL01) AS WorkPerformed,
UDC3.DRDL01 AS CustomerDescription
FROM
SENDTA.F0101 F0101
JOIN
JDEMOD.FSALES2011 FSALES2011
ON
FSALES2011.SXAN8 = F0101.ABAN8
LEFT OUTER JOIN
SENDTA.F0006 F0006
ON
FSALES2011.SXLITM = SUBSTR(F0006.MCMCU,6,7)
LEFT OUTER JOIN
SENDTA.F4801 F4801
ON
FSALES2011.SXLITM = DIGITS(F4801.WADOCO)
LEFT OUTER JOIN
SENCOM.F0005 UDC
ON
F0006.MCRP08 = LTRIM(UDC.DRKY)
AND LTRIM(UDC.DRKY) != ''
AND UDC.DRRT IN ('W1',
'08')
AND UDC.DRSY NOT IN ('30',
'32',
'98')
LEFT OUTER JOIN
SENCOM.F0005 UDC2
ON
F4801.WAWR01 = LTRIM(UDC2.DRKY)
AND LTRIM(UDC2.DRKY) != ''
AND UDC2.DRRT IN ('W1',
'08')
AND UDC2.DRSY NOT IN ('30',
'32',
'98')
LEFT OUTER JOIN
SENCOM.F0005 UDC3
ON
F0101.ABAC15 = LTRIM(UDC3.DRKY)
AND LTRIM(UDC3.DRKY) != ''
AND UDC3.DRRT IN ('15')
謝謝!添加AS允許它運行,現在我的問題是它沒有做我想做的事情!發生了什麼事是F0005對其他表中的某些代碼進行了描述。當我這樣做時,所有的描述都是NULL。 – jcervantes 2012-02-15 15:37:15
一次一個一頁地瀏覽您的查詢。通過從一個小查詢開始,然後添加連接,您可以看到非確定行爲發生在哪一點。這個連接是你需要調查的。這些字段中的值是您期望的值嗎? (例如,當您將ABAC15加入LTRIM(DRKY)時,您是否檢查過實際上是否有任何匹配項,也許您還需要LTRIM(ABAC15)? – MatBailie 2012-02-15 16:10:05
@jcevantes您說的描述是NULL,您確定那裏是對你加入的記錄的描述嗎? – Taryn 2012-02-15 16:10:16