LEFT JOIN上的嵌套Select语句

时间:2012-02-15 15:15:02

标签: sql sql-server tsql

我遇到了正确过滤表格F0005的问题。我尝试过很多种不同的查询方法。我想要完成的是在加入之前对F0005进行过滤。

这就是我希望它被过滤的结果,结果在那张桌子上是完美的。

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不在* N中的表F0005FILTERED中。

感谢您的帮助,我对此有点新鲜,并且没有想法! :)

非常感谢大家,我的问题是我的嵌套选择的格式化,但也有一些完全不同的东西。我加入了错误的字段,所以没有匹配,这就是为什么我得到了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')

4 个答案:

答案 0 :(得分:9)

如果在函数中包装字段,则需要在之后命名。

LTRIM(F0005.DRKY),更改为LTRIM(F0005.DRKY) AS DRKY,

答案 1 :(得分:6)

在嵌套查询中,您缺少DRKY的别名:

SELECT
            LTRIM(F0005.DRKY) as 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

如果在没有返回LTRIM(F0005.DRKY)

的列名时没有别名

答案 2 :(得分:6)

您需要在过滤后的表格中命名计算列。

SELECT LTRIM(F0005.DRKY) AS 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')

请注意AS DRKY

答案 3 :(得分:3)

你真的需要重新格式化你的问题,但我相信我知道你哪里出错了。

您的表别名F0005Filtered不是JDE用户定义代码表的别名。您已经创建了一个子选择,这是F0005Filtered实际代表的。

此外,在该子选择内,您没有名为DRKY的列。

您正在该字段上使用TRIM功能而未指定列名。

要修复,只需重新创建别名: -

LTRIM(F0005.DRKY) AS DRKY

修改

回应用户提问。

为什么不将IN语句作为JOIN条件的一部分?

LEFT OUTER JOIN
    SENCOM.F0005 UDC
ON  LTRIM(UDC.DRKY) != ''
AND ABAC15 = TRIM(UDC.DRKY)
AND UDC.DRRT IN ('W1','08')
AND UDC.DRSY NOT IN ('30', '32','98')

用户定义的代码表往往不会那么大。我为一家大型跨国公司做了一些工作,我们的工作量大约为70K。