简体   繁体   English

选择以分钟为单位的日期时间差,该时间差小于特定的分钟数

[英]Select Date Time Difference in Minutes which is Less than Particular Minutes

I have a query like this: 我有这样的查询:

SELECT DATEDIFF(minute,t.Paydate,t.DelDate) as Mtime 
       FROM Transaction_tbl t WHERE Locid=6 

This will return result like: 这将返回如下结果:

Mtime
8
2
10
20
15
7
6

But, in that query I have to give condition like I want to show result like less than 10 minutes or less than 15 minutes only . 但是,在该查询中,我必须给出类似条件,例如仅显示少于10分钟或少于15分钟的结果。 I have one more table which is location . 我还有一张桌子是位置 In that, I have a column timeinterval with data type Time(0) . 在那,我有一个列timeinterval ,数据类型为Time(0) Actually this value I want to pass in the same query. 实际上,我要在同一查询中传递此值。 So how I can re-write my query showing result less than 10 minutes? 那么,如何在不到10分钟的时间内重写查询结果呢?

Add AND condition in WHERE clause : WHERE子句中添加AND条件:

SELECT  
    DATEDIFF(MINUTE, t.Paydate, t.DelDate) AS Mtime 
FROM Transaction_tbl t 
WHERE 
    Locid = 6
    AND DATEDIFF(MINUTE,t.Paydate,t.DelDate) < 10

As per your new doubt: 根据您的新疑问:

SELECT  
    DATEDIFF(MINUTE, t.Paydate, t.DelDate) AS Mtime,
    COUNT(*) AS Count 
FROM Transaction_tbl t 
WHERE 
    Locid = 6
GROUP BY Mtime
HAVING DATEDIFF(MINUTE, t.Paydate, t.DelDate) < 10

you can do something like this 你可以做这样的事情

SELECT  
   case when ( DATEDIFF(MINUTE, t.Paydate, t.DelDate)<10) then DATEDIFF(MINUTE, t.Paydate, t.DelDate) else null end AS lessthan10,
case when ( DATEDIFF(MINUTE, t.Paydate, t.DelDate)>10) then DATEDIFF(MINUTE, t.Paydate, t.DelDate) else null end AS greaterthan10,
    COUNT(*) AS Count 
FROM Transaction_tbl t 
WHERE 
    Locid = 6
GROUP BY Mtime
HAVING DATEDIFF(MINUTE, t.Paydate, t.DelDate) < 10

Try this. 尝试这个。

WITH CTE(Mtime) As (
select DATEDIFF(minute,t.Paydate,t.DelDate) as Mtime 
from Transaction_tbl  t
)
Select Mtime from 
CTE
Where Mtime < 10

Reference : Using Common Table Expressions 参考: 使用公用表表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM