如何在通过别名创建的列名上放置条件

  • 本文关键字:条件 创建 别名 sql-server
  • 更新时间 :
  • 英文 :


如果我在运行时创建列,如何在列上放置条件? 假设我有一个员工表,其中包含empnameempsal

现在我正在编写一个查询,如下所示:

select empname, (empsal*2) as 'doublesalary' from employee where doublesalary > 30000

我可以简单地做到这一点,就像:

select empname, (empsal*2) as 'doublesalary' from employee where (sal*2) > 30000

但是,如果我想在该别名列上放置条件怎么办?

有什么办法吗??

如果在此示例中避免代码重复非常重要,则可以创建一个视图

如果这看起来工作量太大,您可以从这样的选择中进行选择:

select empname, doublesalary
from (
    select empname, (empsal*2) as doublesalary from employee
) as employee2
where doublesalary > 30000;

这是从选择答案中选择的小提琴。

最新更新