我正在尝试以下 SQL 查询:
select (execution_end_timestamp - execution_queued_timestamp) as totalTime, execution_queued_timestamp from exec_queue
where execution_name like '%Generate%'
and execution_queued_timestamp > '2012-10-04 20:00:00.000'
having totalTime < '1900-01-01 00:00:06.000'
我试过在 where 和 having 子句中使用 totalTime,但在这两种情况下都不起作用。我还尝试了 datediff,希望它能奏效,但结果相同。
是否有可以在 where 或 having 子句中使用计算字段的技巧?谷歌搜索没有发现任何东西,除非在使用聚合函数的情况下。
最佳答案
不,WHERE
子句不允许使用别名,试试,
select (execution_end_timestamp - execution_queued_timestamp) as totalTime, execution_queued_timestamp
from exec_queue
where execution_name like '%Generate%' AND
execution_queued_timestamp > '2012-10-04 20:00:00.000' AND
(execution_end_timestamp - execution_queued_timestamp) < '1900-01-01 00:00:06.000'
ALIAS
对 WHERE
和 HAVING
子句不起作用的原因,
- 首先形成
from子句
中所有表的乘积。 - 然后评估
where 子句
以消除不满足 search_condition 的行。 - 接下来,使用
group by 子句
中的列对行进行分组。 - 然后,不满足
having 子句
e 中的 search_condition 的组将被淘汰。 - 接下来,计算
select 子句
目标列表中的表达式。 - 如果
distinct 关键字
出现在 select 子句中,现在将消除重复行。 union
在每个子选择被求值之后被采用。- 最后,结果行根据
order by 子句
中指定的列进行排序。
关于sql - 是否可以在 SQL Server 中的 where 或 having 子句中使用自定义字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12738503/