简体   繁体   English

使用CASE时无法对列别名进行ORDER BY

[英]Unable to ORDER BY on Column Alias when using CASE

I have a column that will show 'active' if the 'Expiration Date' in the empTable is < Current Date, and will show 'inactive' otherwise, then I want to sort that column so that all 'active' employees will be at the top. 我有一个列,如果empTable中的“到期日期”为<当前日期,则将显示“活动”,否则将显示“非活动”,那么我想对该列进行排序,以便所有“活动”员工都位于最佳。

CREATE PROCEDURE SOME_SP
@SortBy VARCHAR(10)
AS
SELECT emp.empname, 
CASE WHEN (emp.expiration_date < CURRENT_TIMESTAMP) THEN 'Active'   ELSE 'InActive' END AS Emp_Status, DeptName
FROM empTable emp, Dept dpt
WHERE emp.ID = dpt.ID
CASE @sortBy WHEN 'NAME' THEN emp.empName END,
CASE @sortBy WHEN 'STATUS' THEN Emp_Status END

If the user enter 'NAME', then the SP will sort by emp.empName , which works fine, but not the STATUS 如果用户输入“ NAME”,则SP将按emp.empName排序,这可以正常工作,但不能使用STATUS

I get an error saying that Invalid column name 'Emp_Status' 我收到一条错误消息,指出无效的列名'Emp_Status'

What did I do wrong? 我做错了什么?

Edited: I'm so sorry, I realize this query works if it is in plain SQL. 编辑:很抱歉,我意识到此查询在纯SQL中有效。 However, the fact is I'm doing it in the Stored Procedure where the user can specify which column to sort. 但是,事实是我在存储过程中执行此操作,用户可以在其中指定要排序的列。 I will post a more complete SP above. 我将在上面发布更完整的SP。

Okay, here are some comments that I have. 好的,这是我的一些评论。

1 - Please get away from old style joins. 1-请远离老式的联接。 Use the INNER JOIN ON clause. 使用INNER JOIN ON子句。

2 - There is no reason why an alias can not be used in the ORDER BY clause. 2-没有理由不能在ORDER BY子句中使用别名。 Please see Itzik Ben-Gans posting on logical processing order. 请参阅Itzik Ben-Gans关于逻辑处理顺序的过帐。 The SELECT arguments are processed way before the ORDER BY. SELECT参数在ORDER BY之前进行处理。

http://www.sql.co.il/books/insidetsql2008/Logical%20Query%20Processing%20Poster.pdf http://www.sql.co.il/books/insidetsql2008/Logical%20Query%20Processing%20Poster.pdf

3 - Last but not least, a simple example (adventureworks) that make everyone with a hire date less than 2004 as Active, everyone else is in-active. 3-最后但并非最不重要的一个简单示例(冒险工厂),该示例使雇用日期少于2004年的每个人都为“活动”,其他人都处于非活动状态。 This will sort by the status column. 这将按状态列排序。

Good luck. 祝好运。

John 约翰

-- Sample database
Use AdventureWorks2012
GO

-- Sample select showing alias works fine in a order by clause.
SELECT [LoginID] as login_id,
    CASE WHEN (e.HireDate < '20040101') THEN 'Active' 
    ELSE 'InActive' END AS emp_status
FROM [HumanResources].[Employee] as e
ORDER BY emp_status desc
GO

Since you changed your code above, here is a new answer to match. 由于您在上面更改了代码,因此这里有一个新的答案要匹配。 For a CASE statement on an ORDER BY you have to use the actual columns. 对于ORDER BY上的CASE语句,您必须使用实际的列。 For just a simple ORDER BY, the alias will work. 对于一个简单的ORDER BY,别名将起作用。

SO THE ANSWER is it ALL DEPENDS!! 因此,答案取决于一切!!

Use AdventureWorks2012
GO

ALTER PROCEDURE usp_Sort_By_Column(@sort varchar(25))
AS
SELECT 
    [LoginID] as login_id,
    CASE WHEN (e.HireDate < '20040101') 
    THEN 'Active' ELSE 'InActive' END AS emp_status
FROM 
    [HumanResources].[Employee] as e
ORDER BY 
   (CASE 
       WHEN @sort = 'ID' THEN [LoginID] ELSE 
        (CASE WHEN (e.HireDate < '20040101') 
        THEN 'Active' ELSE 'InActive' END)
     END) 
GO

usp_Sort_By_Column 'STATUS'

Link to ORDER BY - Books On Line ... 链接到ORDER BY-在线书籍...

http://msdn.microsoft.com/en-us/library/ms188385.aspx http://msdn.microsoft.com/en-us/library/ms188385.aspx

Best answer for your crazy query: Make a sort column that is dynamic using the variable. 疯狂查询的最佳答案:使用该变量创建一个动态的排序列。 Just order by the first column. 只需在第一列订购即可。 Cleanest answer. 最干净的答案。

Use AdventureWorks2012
GO

ALTER PROCEDURE usp_Sort_By_Column(@sort varchar(25))
AS
SELECT 
    (CASE 
       WHEN @sort = 'ID' THEN [LoginID] ELSE 
        (CASE WHEN (e.HireDate < '20040101') 
        THEN 'Active' ELSE 'InActive' END)
     END) as Sort_Column,

    [LoginID] as login_id,
    CASE WHEN (e.HireDate < '20040101') 
    THEN 'Active' ELSE 'InActive' END AS emp_status
FROM 
    [HumanResources].[Employee] as e
ORDER BY 
   1
GO

usp_Sort_By_Column 'ID'

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

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