是否可以在SQL Server中的where或having子句中使用自定义字段?
我正在尝试以下SQL查询:是否可以在SQL Server中的where或having子句中使用自定义字段?,sql,sql-server,sql-server-2008-r2,Sql,Sql Server,Sql Server 2008 R2,我正在尝试以下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 t
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'
从exec\u队列中选择(execution\u end\u timestamp-execution\u queued\u timestamp)作为totalTime、execution\u queued\u timestamp
其中执行名为“%Generate%”
和执行队列时间戳>'2012-10-04 20:00:00.000'
总时间<'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'
选择(执行结束时间戳-执行排队时间戳)作为总时间、执行排队时间戳
从exec_队列
其中执行名为“%Generate%”和
执行队列时间戳>'2012-10-04 20:00:00.000'和
(执行结束时间戳-执行排队时间戳)<'1900-01-01 00:00:06.000'
ALIAS
在WHERE
和HAVING
子句上不起作用的原因
- 首先,形成
中所有表的乘积from子句
- 然后计算
,以消除不满足搜索条件的行where子句
- 接下来,使用
groupby子句中的列对行进行分组
- 然后,排除在具有claus的
中不满足搜索条件的组
- 接下来,计算
target列表中的表达式select子句
- 如果select子句中存在
,则现在将消除重复行distinct关键字
- 在对每个子选择进行评估后,将采用
联合
- 最后,根据
order by子句中指定的列对结果行进行排序
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'
选择(执行结束时间戳-执行排队时间戳)作为总时间、执行排队时间戳
从exec_队列
其中执行名为“%Generate%”和
执行队列时间戳>'2012-10-04 20:00:00.000'和
(执行结束时间戳-执行排队时间戳)<'1900-01-01 00:00:06.000'
ALIAS
在WHERE
和HAVING
子句上不起作用的原因
- 首先,形成
中所有表的乘积from子句
- 然后计算
,以消除不满足搜索条件的行where子句
- 接下来,使用
groupby子句中的列对行进行分组
- 然后,排除在具有claus的
中不满足搜索条件的组
- 接下来,计算
target列表中的表达式select子句
- 如果select子句中存在
,则现在将消除重复行distinct关键字
- 在对每个子选择进行评估后,将采用
联合
- 最后,根据
order by子句中指定的列对结果行进行排序
1. FROM/JOIN
2. WHERE/ON -- exception for LEFT JOIN
3. GROUP BY (incl. CUBE, ROLLUP, GROUPING SETS)
4. HAVING
5. SELECT
6. DISTINCT
7. ORDER BY
因此,为SELECT列创建的别名对于WHERE和HAVING阶段不可见。它实际上只是复制和粘贴表达式,非常琐碎。边缘情况可能是在处理长而复杂的公式时,最好使用子查询,例如
select totalTime,
execution_queued_timestamp
from (
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'
) x
where totalTime < '1900-01-01 00:00:06.000'
选择totalTime,
执行队列时间戳
从(
选择(执行\u结束\u时间戳-执行\u排队\u时间戳)作为totalTime,
执行队列时间戳
从exec_队列
其中执行名为“%Generate%”
和执行队列时间戳>'2012-10-04 20:00:00.000'
)x
其中总时间<'1900-01-01 00:00:06.000'
我给你一个提示。SQL Server实际上知道将WHERE过滤器引入内部查询,并对基表应用它,因此不会损失那里的性能 我以前有SQL-92、99和2003 ISO标准的链接,但现在可以了 基本上,查询执行的顺序是
1. FROM/JOIN
2. WHERE/ON -- exception for LEFT JOIN
3. GROUP BY (incl. CUBE, ROLLUP, GROUPING SETS)
4. HAVING
5. SELECT
6. DISTINCT
7. ORDER BY
因此,为SELECT列创建的别名对于WHERE和HAVING阶段不可见。它实际上只是复制和粘贴表达式,非常琐碎。边缘情况可能是在处理长而复杂的公式时,最好使用子查询,例如
select totalTime,
execution_queued_timestamp
from (
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'
) x
where totalTime < '1900-01-01 00:00:06.000'
选择totalTime,
执行队列时间戳
从(
选择(执行\u结束\u时间戳-执行\u排队\u时间戳)作为totalTime,
执行队列时间戳
从exec_队列
其中执行名为“%Generate%”
和执行队列时间戳>'2012-10-04 20:00:00.000'
)x
其中总时间<'1900-01-01 00:00:06.000'
我给你一个提示。SQL Server实际上知道将WHERE过滤器引入内部查询,并对基表应用它,因此不会损失那里的性能 谢谢,这很好用,我不知道为什么我没有想到这一点。我通常将原始查询设为派生表,然后在派生表返回的别名上使用where子句。这被认为是低效的吗?@MrMoose由于您在额外的临时表中添加了它,所以性能有所降低。汉克斯·约翰:这取决于你对性能和清晰度的选择。如果我能在性能和清晰度之间做出选择那就太好了:)@John-care来支持这句话
它会在一定程度上降低性能吗?谢谢,这很好,我不知道为什么我没有想到这一点。我通常会这样做