我在SQL上运行查询时遇到了困难。我需要从我已在子查询中创建的字段中删除最后3个字符,然后根据该新字段加入。验证在第5行和第34行无效;无效的列名称' PCD'。"
SELECT
*, LEFT(SUBQUERY.PC,Len(SUBQUERY.PC) -3) AS PCD
FROM
(SELECT
PI.ClientPostcode, PI.casekey,
REPLACE( PI.ClientPostcode ,' ', '') As PC
FROM PI
WHERE LEN(PI.ClientPostcode) >= 5) AS SUBQUERY
INNER JOIN
[Postcodes] On outcode = PCD
任何帮助将不胜感激
答案 0 :(得分:0)
您无法在定义的同一级别上使用别名。
SELECT *,
LEFT(SUBQUERY.pc, Len(SUBQUERY.pc) - 3) AS PCD
FROM (SELECT pi.clientpostcode,
pi.casekey,
Replace(pi.clientpostcode, ' ', '') AS PC
FROM pi
WHERE Len(pi.clientpostcode) >= 5) AS SUBQUERY
INNER JOIN [postcodes]
ON outcode = LEFT(SUBQUERY.pc, Len(SUBQUERY.pc) - 3)
^^^^ dont use the alias
或者你在像这样的子查询中使用它
SELECT *
FROM (
SELECT *,
LEFT(SUBQUERY.pc, Len(SUBQUERY.pc) - 3) AS PCD
FROM (SELECT pi.clientpostcode,
pi.casekey,
Replace(pi.clientpostcode, ' ', '') AS PC
FROM pi
WHERE Len(pi.clientpostcode) >= 5) AS SUBQUERY
) other_subq
INNER JOIN [postcodes]
ON outcode = other_subq.PCD