使用方程datediff过滤数据的困惑

问题描述

我在编写查询时遇到了麻烦,该查询将使用或条件来计算30分钟或120分钟的小时数,但是我一直遇到错误。我为方程式使用了别名。为方程式设置or条件的正确方法是什么。

-今天,当子工作进入压区时,不再压在压区中了,压在压区中的时间长了

SELECT DATEDIFF(minute,begintime,aftertime) difference 






  FROM [ABC].[dbo].[timerecords] (nolock)



  where begintime > '1994-08-11'

  and aftertime is not null
 
  -- you can not use alias
  DATEDIFF(minute,aftertime) in (30 or 120)

解决方法

将查询打包为派生表

select * from
(
  SELECT DATEDIFF(minute,begintime,aftertime) difference 
  FROM [ABC].[dbo].[timerecords] (nolock)
  where begintime > '1994-08-11'
    and aftertime is not null
) dt
where difference in (30,120)