简体   繁体   English

使用where子句会导致无效的列名错误

[英]using where clause causes invalid column name error

I am using SQL Server 2012. 我正在使用SQL Server 2012。

I have written the query below which works fine apart from when I include the last line 'where NomDiff <> 0'. 我写了下面的查询,它与最后一行'where NomDiff <> 0'分开时效果很好。

It tells me NomDiff is an invalid column name. 它告诉我NomDiff是无效的列名。 I don't understand why and don't know how to get the query to return only rows where the nomDiff is not equal to zero? 我不明白为什么,也不知道如何使查询仅返回nomDiff不等于零的行?

;with pf as
(
    select Name, Sedol, Nominal from tblTempPLF 
    where FundCode = 'CSGE'
), pc as
(
    select Name, Sedol, Nominal from tblTempPCF 
    where FundCode = 'BTCM'
)
select coalesce(pf.Name, pc.Name) Name, coalesce(pf.Sedol, pc.Sedol) Sedol, 
isnull(pf.Nominal,0) PfNom, isnull(pc.Nominal,0) PcNom, isnull(pf.Nominal,0) - isnull(pc.Nominal,0) NomDiff
from pf full outer join pc on pf.Sedol = pc.Sedol
where NomDiff <> 0 

Using APPLY VALUES is a good way of not having to use a subquery, or another CTE: 使用APPLY VALUES是无需使用子查询或其他CTE的好方法:

WITH    pf
AS ( SELECT
      Name ,
      Sedol ,
      Nominal
      FROM tblTempPLF
      WHERE FundCode = 'CSGE'
    ),
pc
AS ( SELECT
      Name ,
      Sedol ,
      Nominal
      FROM tblTempPCF
      WHERE FundCode = 'BTCM'
    )
  SELECT
      Name = COALESCE(pf.Name, pc.Name),
      Sedol = COALESCE(pf.Sedol, pc.Sedol),
      PfNom = ISNULL(pf.Nominal, 0),
      PcNom = ISNULL(pc.Nominal, 0),
      NomDiff = Nom.Diff
  FROM pf
  FULL OUTER JOIN pc
  ON  pf.Sedol = pc.Sedol
  CROSS APPLY(VALUES(ISNULL(pf.Nominal, 0) - ISNULL(pc.Nominal, 0))) AS Nom(Diff)
  WHERE Nom.Diff <> 0;

You can't use a previously defined alias in the where clause. 您不能在where子句中使用先前定义的alias Use the calculation instead. 请改用计算。

select coalesce(pf.Name, pc.Name) Name, 
coalesce(pf.Sedol, pc.Sedol) Sedol,   
isnull(pf.Nominal,0) PfNom, isnull(pc.Nominal,0) PcNom, 
isnull(pf.Nominal,0) - isnull(pc.Nominal,0) NomDiff
from pf full outer join pc on pf.Sedol = pc.Sedol
where isnull(pf.Nominal,0) - isnull(pc.Nominal,0)<> 0

You can't use the alias for the column in the WHERE since you are creating it in the SELECT , change to: 您不能在WHERE使用该列的别名,因为您是在SELECT中创建的,请更改为:

WHERE isnull(pf.Nominal,0) - isnull(pc.Nominal,0) <> 0

Or you can use a subquery/cte to make the alias available to an outer query that references it. 或者,您可以使用子查询/ cte使别名可用于引用它的外部查询。

The reason for this is that WHERE is actually evaluated prior to SELECT , the optimizer filters out rows before worrying about which fields to return. 这是因为WHERE实际上是在SELECT之前评估的,优化程序在担心返回哪些字段之前会过滤掉行。

Just add another cte so you can use the alias 只需添加另一个CTE,即可使用别名

;with pf as
(
    select Name, Sedol, Nominal from tblTempPLF 
    where FundCode = 'CSGE'
), pc as
(
    select Name, Sedol, Nominal from tblTempPCF 
    where FundCode = 'BTCM'
), anotherOne as /* add another cte */
(
    select 
        coalesce(pf.Name, pc.Name) Name, 
        coalesce(pf.Sedol, pc.Sedol) Sedol, 
        isnull(pf.Nominal,0) PfNom, 
        isnull(pc.Nominal,0) PcNom, 
        isnull(pf.Nominal,0) - isnull(pc.Nominal,0) NomDiff
    from pf full 
    outer join pc 
        on pf.Sedol = pc.Sedol
)
select *
from anotherOne
where NomDiff <> 0 

Since you are already using CTE , it would be easier to just wrap that outer query as another CTE and then filter out the NomDiff (alias for the calculated column). 由于您已经在使用CTE ,因此将外部查询包装为另一个CTE然后过滤掉NomDiff (计算列的别名)会更容易。 Alias does not scope within the same select statement, so in order to use an Alias directly, you would need to wrap that either in a subquery or a CTE . Alias不在同一select语句内,因此,为了直接使用Alias ,您需要将其包装在subqueryCTE Otherwise, you could also directly use the calculation in the where clause like others have suggested here. 否则,您也可以像其他建议一样直接在where子句中使用计算。

;WITH pf
AS (
    SELECT NAME
        ,Sedol
        ,Nominal
    FROM tblTempPLF
    WHERE FundCode = 'CSGE'
    )
    ,pc
AS (
    SELECT NAME
        ,Sedol
        ,Nominal
    FROM tblTempPCF
    WHERE FundCode = 'BTCM'
    )
    ,calc
AS (
    SELECT coalesce(pf.NAME, pc.NAME) NAME
        ,coalesce(pf.Sedol, pc.Sedol) Sedol
        ,isnull(pf.Nominal, 0) PfNom
        ,isnull(pc.Nominal, 0) PcNom
        ,isnull(pf.Nominal, 0) - isnull(pc.Nominal, 0) NomDiff
    FROM pf
    FULL OUTER JOIN pc ON pf.Sedol = pc.Sedol
    )
SELECT *
FROM calc
WHERE NomDiff <> 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM