0

我在 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 都可以帮助我在我犯错的地方吗

4

3 回答 3

2

嗨看看这个:

SQLFIDDLE

不是最有效的,我相信它对于代码来说很丑陋。但是把它放在上面让你看到步骤。你可能会从中得到一个想法。主要是使用算术运算符来获得结果。有一些额外的字段,因为我主要使用它来向您展示数据流。我确信其他方法在性能方面非常有效。你可以试试。

询问:

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
 ;

结果

  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
 ;
于 2012-12-04T08:10:03.603 回答
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
于 2012-12-04T07:35:04.720 回答
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,您就得到了小时或分钟,而使用%,您就得到了剩余值。

于 2012-12-04T07:39:01.303 回答