[英]Using SQL column Alias in built-in function?
I'm trying to order a selection based on the alias but I can't figure out how. 我正在尝试根据别名订购一个选择,但是我不知道如何选择。 Here is an example:
这是一个例子:
select distinct top 100 id,
col1,
col2,
CASE WHEN @orderFormat = 'this' then col1
WHEN @orderFormat = 'that' then col2
END as orderby
from table
where col1 = like '%'
order by Len(orderby) asc, orderby asc
Whenever I pass my alias 'orderby' as an argument, it is reported as an invalid column. 每当我将别名“ orderby”作为参数传递时,都将其报告为无效列。
My goal is to be able to order a variable column alphanumerically. 我的目标是能够按字母顺序对可变列进行排序。 I know that 'order by Len(orderby) asc, orderby asc works, but just not with an alias.
我知道'由Len(orderby)asc排序,orderby asc起作用,但不能使用别名。
Anybody know a good way around this or if I'm doing something wrong? 有谁知道解决这个问题的好方法,或者我做错了什么?
Thanks! 谢谢!
EDIT: 编辑:
I've managed to strip the select function down to this: 我设法剥离选择功能到此:
select top 200 Clip_Name as orderby
from Clips
order by Len(orderby) asc, orderby asc
Clip_Name is declared as column Clip_Name(nvarchar, not null)
. Clip_Name被声明为
column Clip_Name(nvarchar, not null)
。 The error from Microsoft SQL Server 2008 R2 Edition is Msg 207, Level 16, State 1, Line 1 Invalid column name 'orderby'
. Microsoft SQL Server 2008 R2 Edition的错误是
Msg 207, Level 16, State 1, Line 1 Invalid column name 'orderby'
。
However, this works (without the alias): 但是,这有效(不使用别名):
select top 200 Clip_Name
from Clips
order by len(FLE_ID) desc, FLE_ID desc
When you're using DISTINCT
, you can only order by expressions that are actually in the SELECT
list. 使用
DISTINCT
,只能按SELECT
列表中实际存在的表达式进行排序。 You can't reference columns, aliases or expressions that aren't there. 您不能引用不存在的列,别名或表达式。 Here's one possible workaround, though it might actually be better to simply remove the
DISTINCT
(if you have two rows with the same id
then there is something seriously wrong with either your schema or at least the name of that column). 这是一个可能的解决方法,尽管实际上删除
DISTINCT
可能更好(如果您有两行具有相同的id
那么您的架构或至少该列的名称存在严重问题)。
select distinct top 100 id,
col1,
col2,
CASE WHEN @orderFormat = 'this' then col1
WHEN @orderFormat = 'that' then col2
END as orderby,
len(CASE WHEN @orderFormat = 'this' then col1
WHEN @orderFormat = 'that' then col2
END) AS ignore_this_column
from table
where col1 like '%'
order by ignore_this_column, orderby;
Expressed much simpler so you don't have to repeat the expression (and also without the unnecessary DISTINCT
): 它的表达要简单得多,因此您不必重复该表达式(也无需重复不必要的
DISTINCT
):
;WITH x AS
(
SELECT id, col1, col2,
orderby = CASE @orderFormat
WHEN 'this' THEN col1
WHEN 'that' THEN col2
END
FROM dbo.table
WHERE col1 LIKE '%' -- necessary?
)
SELECT id, col1, col2
FROM x
ORDER BY LEN(orderby), orderby;
Based on you're first query, and following discussion that DISTINCT needs to be removed, this will work: 根据您的第一个查询,并根据需要删除DISTINCT的讨论,这将起作用:
select top 100 id,
col1,
col2,
CASE WHEN @orderFormat = 'this' then col1
WHEN @orderFormat = 'that' then col2
END as orderby
from t
order by Len(CASE WHEN @orderFormat = 'this' then col1
WHEN @orderFormat = 'that' then col2
END) asc, orderby asc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.