『壹』 如何用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)