SELECT列包含值> 1000

时间:2011-09-12 07:07:35

标签: sql

我正在使用SQL查询

select 
    datediff(hour,Convert(DateTime,Convert(DateTime, Convert(nVarChar, cast(complaintdate as datetime), 101)) + '' +  
    Convert(varchar,complainttime,114)),dateadd(mi,330,GETUTCDATE())) 
from complaintregister

它会创建一个类似

的列
No column name
  15455
  15259
  15190
  15190
  15165
   300
   200
  1000
   500

我想要一个包含值的列> 1000

2 个答案:

答案 0 :(得分:5)

在条件中重复表达式:

select datediff(hour,Convert(DateTime,Convert(DateTime, Convert(nVarChar, cast(complaintdate as datetime), 101)) +''+  Convert(varchar,complainttime,114)),dateadd(mi,330,GETUTCDATE()))
from complaintregister
where datediff(hour,Convert(DateTime,Convert(DateTime, Convert(nVarChar, cast(complaintdate as datetime), 101)) +''+  Convert(varchar,complainttime,114)),dateadd(mi,330,GETUTCDATE())) > 1000

查询规划器应该识别表达式是相同的,实际上只为每条记录计算一次。

或者,您可以使用嵌套查询:

select time
from (
  select datediff(hour,Convert(DateTime,Convert(DateTime, Convert(nVarChar, cast(complaintdate as datetime), 101)) +''+  Convert(varchar,complainttime,114)),dateadd(mi,330,GETUTCDATE())) as time
  from complaintregister
) x
where time > 1000

答案 1 :(得分:0)

如果它是SQL服务器,你可以点这样的东西

    select tablename.colname from 
(select datediff(hour,Convert(DateTime,Convert(DateTime, Convert(nVarChar, cast(complaintdate as datetime), 101)) +''+  Convert(varchar,complainttime,114)),dateadd(mi,330,GETUTCDATE())) as colname
from complaintregister) as tablename
where tablename.colname > 1000

没有测试db atm,所以你必须稍微玩一下