① 求時間差的sql語句。 比如如下數據
select
sum(datediff(millisecond,createtime,
closetime))as
總豪秒數
from
表名
where
msisdn=138
select
sum(datediff(minute,createtime,
closetime))as
總秒數
from
表名
where
msisdn=138
select
sum(datediff(minute,createtime,
closetime))as
總分鍾
from
表名
where
msisdn=138
select
sum(datediff(Hour,createtime,
closetime))as
總小時
from
表名
where
msisdn=138
select
sum(datediff(Day,createtime,
closetime))as
總天數
from
表名
where
msisdn=138
select
sum(datediff(week,createtime,
closetime))as
總天數
from
表名
where
msisdn=138
select
sum(datediff(Month,createtime,
closetime))as
總天數
from
表名
where
msisdn=138
select
sum(datediff(quarter,createtime,
closetime))as
總季數
from
表名
where
msisdn=138
select
sum(datediff(year,createtime,
closetime))as
總年數
from
表名
where
msisdn=138
樓主沒有說
時間單位
,我列出所有,你自己選
② sql怎麼計算時間差
DATEDIFF(mi,開始時間,結束時間)
這個函數可以算出會話時間,mi代表分鍾,
1。兩張表匹配下,sum(算出的會話時間)即可
2.select
訪問客戶,sum(算出的會話時間)
from
...
group
by
訪問客戶
3.用agv(算出的會話時間)
即可
4.select
訪問客戶,agv(算出的會話時間)
from
...
group
by
訪問客戶
③ 用SQL語句怎麼寫時間差
select to_date('20070506','yyyymmdd') - to_date('20070203','yyyymmdd') from al;
這樣查詢出來是兩個時間的差值——以天為單位的,如果需要以小時為單位的,在減法外面加個括弧,再乘以24就可以了,其他時間單位的,自己類推。
可以這樣直接減,是因為oracle在存儲date型時間的時候,內部是使用數字存儲的,所以可以直接減,如果是用timestamp型時間,可能不可以直接減
④ SQL資料庫時間差
先給此表增加一個數字類型的列「Diff」,然執行如下sql語句:
update
[表名]
set
[diff]=datediff(day,[A日期列],[B日期列])
datediff函數的第一個參數設置為day時,計算的是兩個日期以天為單位的差數,設置為month、year時以此類推。
⑤ 在Sql語句中怎樣計算出兩個日期的差值
sql語句中計算兩個日期的差值用datediff函數。
工具:sqlserver 2008 R2
步驟:
1、計算2009-06-23與2009-06-17之間的天數。語句如下:
selectdatediff(d,'2009-06-17','2009-06-23')
2、查詢結果:
⑥ 查詢兩個日期的時間差(精確到小時),同時剔除日期之間的周六周日,SQL語句怎麼寫
先創建一個函數計算時間差
Alter function dbo.fn_test(@begin datetime,@end datetime)
returns int
As
BEGIN
declare @i int,@j int
set @i=0
set @j=0
if @end<@begin
begin
declare @t datetime
set @t=@end
set @end=@begin
set @begin=@t
end
while dateadd(hh,@i,@begin) <@end
begin
if datepart(weekday,dateadd(hh,@i,@begin)) not in(1,7)
set @j=@j+1
set @i=@i+1
end
return @j
end
調用函數
select dbo.fn_test(@begin,@end)
⑦ 選出時間差不超過十秒的數據,SQL怎麼寫
declare @t table(id int identity(1,1), [name] varchar(50), [time] datetime)
insert into @t
select [name], [time] from yourtablename
order by [time]
select a.[name], a.[time]
from @t A
inner join @t B
on A.id = B.id -1
and datediff(second, A.time, B.time) < 10
union all
select B.[name], B.[time]
from @t A
inner join @t B
on A.id = B.id -1
and datediff(second, A.time, B.time) < 10
⑧ 用SQL語句怎麼寫時間差少於18秒
計算時間差是Oracle DATA數據類型的一個常見問題。Oracle支持日期計算,你可以創建諸如「日期1-日期2」這樣的表達式來計算這兩個日期之間的時間差。
一旦你發現了時間差異,你可以使用簡單的技巧來以天、小時、分鍾或者秒為單位來計算時間差。為了得到數據差,你必須選擇合適的時間度量單位,這樣就可以進行數據格式隱藏。
計算時間差是Oracle DATA數據類型的一個常見問題。Oracle支持日期計算,你可以創建諸如「日期1-日期2」這樣的表達式來計算這兩個日期之間的時間差。
一旦你發現了時間差異,你可以使用簡單的技巧來以天、小時、分鍾或者秒為單位來計算時間差。為了得到數據差,你必須選擇合適的時間度量單位,這樣就可以進行數據格式隱藏。
使用完善復雜的轉換函數來轉換日期是一個誘惑,但是你會發現這不是最好的解決方法。
round(to_number(end-date-start_date))- 消逝的時間(以天為單位)
round(to_number(end-date-start_date)*24)- 消逝的時間(以小時為單位)
round(to_number(end-date-start_date)*1440)- 消逝的時間(以分鍾為單位)
⑨ 任意輸入的兩個日期的時間差,用sql寫
--兩個時間相減得到的結果是天,如果要取小時則將結果集乘24,取分再乘60,取秒再乘60.....
select to_date('2016-01-05 18:30:00','yyyy-mm-dd hh:24:mi') - to_date('2016-01-05 10:30:00','yyyy-mm-dd hh:24:mi') from al;
select (to_date('2016-01-05 11:30:20','yyyy-mm-dd hh:24:mi') - to_date('2016-01-05 10:30:00','yyyy-mm-dd hh:24:mi'))*24*60*60 from al;
⑩ sql語句書寫,求時間存在時對應的時間之差。謝謝
可以使用case when。。。 then。。。 else。。。end語句