用于查找两次和总和的差异的SQL查询

时间:2012-12-04 06:20:21

标签: sql-server

我在SQL Server中有这个表:

UserId    Date                       Start Time              End Time
------------------------------------------------------------------------------------
20       2012-04-02 00:00:00.000     NULL                    2012-04-02 09:17:57.000
20       2012-04-02 00:00:00.000    2012-04-02 09:17:57.000  2012-04-02 09:57:55.000
20       2012-04-02 00:00:00.000    2012-04-02 09:57:55.000  2012-04-02 10:04:58.000
20       2012-04-02 00:00:00.000    2012-04-02 10:04:58.000  2012-04-02 10:21:40.000
20       2012-04-02 00:00:00.000    2012-04-02 10:22:15.000  2012-04-02 10:22:20.000
20       2012-04-02 00:00:00.000    2012-04-02 10:22:56.000  2012-04-02 10:23:33.000

我想找到开始时间和结束时间之间的差异,并根据日期总结差异时数

所需的输出是:

UserID    Time_Duration
-----------------------
 20           1:20:20      (this is example not actuals)

任何人都可以帮我写一个SQL查询.. 我尝试过以下查询,

select sum(sub1.TotalSeconds / 3600) as [Hours], sum((sub1.TotalSeconds % 3600) / 60)
as [Minutes], 
sum((sub1.TotalSeconds % 3600) % 60) as [Seconds],sub1.Date
from
(
SELECT
  sub.UserID,
  sub.Date,   
sum(datepart(hour, sub.end_time-sub.start_time) * 3600) + sum(datepart(minute,        sub.end_time-sub.start_time) * 60) +
sum(datepart(second, sub.end_time-sub.start_time)) as TotalSeconds
from.......
) AS sub 
 group by sub.UserID,sub.Date,sub.start_time,sub.end_time)
  as sub1 group by sub1.Date;

我得到以下结果

 Hours   Minutes Seconds     Date
 3      347      515    2012-04-02 00:00:00.000

但是我想添加分钟,如果大于60则需要几小时为3 + 1小时,依此类推。任何1可以帮助我犯错误

3 个答案:

答案 0 :(得分:2)

嗨,看看这个:

SQLFIDDLE

NOT 效率最高,我相信它对代码来说很难看。但是请你把它放在一边看看步骤。你可能会从中得到一个想法。主要使用arimethic运算符来获得结果。我使用它时有额外的字段主要是为了向您显示数据流。我相信其他方法在性能方面效率更高。你可以试试。

查询:

select q.userid, t.date,
q.h + round((q.m + (q.s/60))/60,0) as hh,
round((q.m + (q.s/60)) mod 60,0) as mm,
q.s mod 60 as ss
from 
(select t.userid,t.date, 
sum(t.hours) as h,
sum(t.minutes) as m,
sum(t.seconds) as s
from (select userid,date,
TIMEDIFF(EndTime, StartTime) as duration,
TIMESTAMPDIFF(hour,starttime,endtime) as hours,
TIMESTAMPDIFF(minute,starttime,endtime) mod 60 as minutes,
TIMESTAMPDIFF(second,starttime,endtime) mod 60 seconds
from datestimes) as t
) as q
 ;

Resutls

  USERID    DATE                            HH  MM  SS
  20        April, 02 2012 00:00:00+0000    13  4   25

仅使用一个嵌套查询更新了查询

事实上,您可以使用一个嵌套查询来实现此目的。道歉,因为整个答案是基于MYSQL。所以希望您可以使用SQL Server语法来实现该逻辑:)

select t.userid, t.date, 
(sum(t.hours) + round((sum(t.minutes) +
sum(t.seconds)/60)/60,0)) as h,
(round((sum(t.minutes) +
sum(t.seconds)/60) mod 60,0)) as m,
sum(t.seconds) mod 60 as s

from 

(select userid, date,
TIMEDIFF(EndTime, StartTime) as duration,
TIMESTAMPDIFF(hour,starttime,endtime) as hours,
TIMESTAMPDIFF(minute,starttime,endtime) mod 60 as minutes,
TIMESTAMPDIFF(second,starttime,endtime) mod 60 seconds
from datestimes) as t
 ;

答案 1 :(得分:1)

Declare @test table(start datetime,ende datetime)
insert into @test Values ('20120101 10:00','20120101 18:00')
insert into @test Values ('20120101 10:00','20120101 18:13')
insert into @test Values ('20120101 10:00','20120102 18:25')
insert into @test Values ('20120101 10:00','20120101 18:00')
insert into @test Values ('20120101 10:00','20120101 18:00')


Select 
FLOOR(Cast(Dummy as Float)) as Days
,DATEPART(Hour,dummy) as Hours
,DATEPART(MINUTE,dummy) as Minutes
,DATEPART(Second,dummy) as Seconds
from
(
Select 
DateAdd(second, SUM(DATEDIFF(SECOND,start,ende)),Cast(0 as Datetime)) as Dummy 
from @test
) a

答案 2 :(得分:1)

您可以通过以下方式执行此操作:

Select 
    CAST(DATEDIFF(SECOND, StartDate, EndDate) / 3600 as VARCHAR) + ':' + 
    CAST((DATEDIFF(SECOND, StartDate, EndDate) % 3600) / 60 as VARCHAR) + ':' + 
    CAST(DATEDIFF(SECOND, StartDate, EndDate) % 60 as VARCHAR) as DateDifference
From YourTable

DATEDIFF 函数可以将开始和结束时间之间的秒数差异作为整数值。简单地将该值除以3600或60,您就有几小时或几分钟,而则保留了值。