English 中文(简体)
T-SQL 计算栏目中的栏目——
原标题:T-SQL Column alias on computed column - Invalid column name
  • 时间:2011-07-06 03:13:31
  •  标签:
  • sql
  • tsql

一米,用别物指计算栏。 这里是实际法典Im试图工作、对相似性进行计算和在相同等级为3个或3个以上的情况下返回的配对器。

select [FirstName], difference([FirstName],  mitch ) as similarity
from [Dev].[dbo].[Name]
where similarity > 2
order by similarity desc

特殊信息:

字母名称相似。

由于相似性不是真正的一栏,我如何做这项工作?

最佳回答

插图和计算在选标(<代码>WHERE和)阶段之后进行的查询(。 因此,可在<代码>中提及。 WHERE 条款或载于JOIN中,因为它们尚未存在。 您也可使用<代码>上的询问。 缩略语 条款作为子类,或可重复<代码>中的计算。 WHERE 条款:

select * 

from
(select [FirstName], difference([FirstName],  mitch ) as similarity
from [Dev].[dbo].[Name]) src

where similarity > 2
order by similarity desc

or

select [FirstName], difference([FirstName],  mitch ) as similarity
from [Dev].[dbo].[Name]
where difference([FirstName],  mitch ) > 2
order by similarity desc
问题回答

所有答案都能够解决你的问题,但就复杂局势而言,你只能重复你的询问。

正确的方式是使用。 CROSS and AppLY

select [FirstName], similarity
from [Dev].[dbo].[Name]
cross apply
  (
     select similarity =
     difference([FirstName],  mitch )  
  )computed_column
where similarity > 2
order by similarity desc

页: 1

Try:

SELECT * 
  FROM (
        SELECT [FirstName], difference([FirstName],  mitch ) as similarity
            FROM [Dev].[dbo].[Name]
        ) a
WHERE similarity > 2
ORDER BY similarity desc
select [FirstName], difference([FirstName],  mitch ) as similarity
from [Dev].[dbo].[Name]
where difference([FirstName],  mitch ) > 2
order by 2 desc

You can t reference column aliases in where clause





相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签