‘壹’ 如何用sql语句筛选相差5分钟以内的所有数据
你是这个意思吧:
查询这样的一条数据:存在另外一条数据的时间项和它的时间项相差在5分钟以内。
这个主要是处理日期函数,我用Oracle数据库做,
首先建立测试表:
create table test26
(
mydate date
)
然后插入测试数据:
insert all
into test26 values(to_date('2010-04-01 12:00:00','yyyy-mm-dd hh24:mi:ss'))
into test26 values(to_date('2010-04-01 12:03:00','yyyy-mm-dd hh24:mi:ss'))
into test26 values(to_date('2010-04-02 12:00:00','yyyy-mm-dd hh24:mi:ss'))
into test26 values(to_date('2010-04-03 14:00:00','yyyy-mm-dd hh24:mi:ss'))
into test26 values(to_date('2010-04-03 14:01:00','yyyy-mm-dd hh24:mi:ss'))
into test26 values(to_date('2010-04-03 14:02:00','yyyy-mm-dd hh24:mi:ss'))
into test26 values(to_date('2010-04-06 12:00:00','yyyy-mm-dd hh24:mi:ss'))
into test26 values(to_date('2010-04-07 11:00:00','yyyy-mm-dd hh24:mi:ss'))
into test26 values(to_date('2010-04-07 11:04:59','yyyy-mm-dd hh24:mi:ss'))
into test26 values(to_date('2010-04-08 12:00:00','yyyy-mm-dd hh24:mi:ss'))
select * from al
然后查询这样的一条数据:存在另外一条数据和它相差在5分钟以内。
select to_char(t1.mydate,'yyyy-mm-dd hh24:mi:ss') from test26 t1
where exists
(
select * from test26 t2 where
((t1.mydate-t2.mydate)<(1/(24*12)) and t1.mydate>t2.mydate)
or ((t2.mydate-t1.mydate)<(1/(24*12)) and t2.mydate>t1.mydate)
and t1.mydate<>t2.mydate
)
order by t1.mydate
结果为:
TO_CHAR(T1.MYDATE,'YYYY-MM-DDHH24:MI:SS')
2010-04-01 12:00:00
2010-04-01 12:03:00
2010-04-03 14:00:00
2010-04-03 14:01:00
2010-04-03 14:02:00
2010-04-07 11:00:00
2010-04-07 11:04:59
不同数据库的日期处理函数不一样,还有什么问题给我留言,OK?
‘贰’ sql语句判断当前时间与存储时间的差值怎么写
SELECT
(CASE WHEN DATEDIFF(n,你的字段,GETDATE()) > 5
THEN 1
ELSE 0 END
)AS FLAG
FROM 表
大于5返回1 否则返回 0 n 表示分钟
yyyy 年份
q 季度
m 月(结果是从 1 到 12 之间的数)
y 一年中的某天(从 1 到 365,闰年是从 1 到 366)
d 日期的天部分(1 到 31)
w 一周中的某天(1 到 7,其结果取决于 firstDayOfWeek)
ww 一年中的某周(1 到 53)
h 提取给定日期时间的小时部分(0 到 23)
n 分钟部分(0 到 59)
s 秒钟部分(0 到 59)
‘叁’ SQL,按5分钟统计发送数量,SQL语句怎么写呀
最简单方法,把datetime转成float型(单位整数部分为天),然后乘24*60/5,就是整数部分是5分钟了,然后取整就行了
用这种方法做,随便你算几分钟的分组都能算
如:
select getdate(),cast(floor(cast(getdate() as float)*24*60/5)*5/60/24 as smalldatetime)
----------------------- -----------------------
2010-09-16 19:19:34.547 2010-09-16 19:15:00
(1 行受影响)
select getdate(),substring(convert(varchar,cast(floor(cast(getdate() as float)*24*60/5)*5/60/24 as smalldatetime),120),12,5)
----------------------- ----------
2010-09-16 19:23:47.340 19:20
(1 行受影响)
那么你的这个问题
select
substring(convert(varchar,cast(floor(cast(sendtime as float)*24*60/5)*5/60/24 as smalldatetime),120),12,5)
,sum(concount)
from 表名
group by substring(convert(varchar,cast(floor(cast(sendtime as float)*24*60/5)*5/60/24 as smalldatetime),120),12,5)