SQL-使用汇总来计算工时总和

时间:2018-12-04 11:19:19

标签: sql sql-server rollup

我希望将所有小时的总和作为最后一行的摘要。我正在使用以下查询来获取结果:

create table time_test (type varchar(10),time varchar(10))
insert into time_test values ('A','01:25')
insert into time_test values ('B','02:30')
insert into time_test values ('C','05:56')
insert into time_test values ('D','00:50')

--select * from time_test

SELECT
  type = ISNULL(type, 'Total'),
  time = substring((cast(sum(((cast(substring (time,2,1) as decimal)*60 + cast(substring (time,4,2) as decimal))/60)) as varchar(10))),1,2)+':' + cast((cast((round(((cast((((cast((substring((cast((sum(((cast(substring (time,2,1) as decimal)*60 + cast(substring (time,4,2) as decimal))/60))) as varchar(10))),4,2)) as int))*60)) as decimal)/100)),0)) as int)) as varchar(10))
FROM time_test
GROUP BY ROLLUP(type);

输出:

enter image description here

如您所见,除了总体计算工作正常之外,时间还不正确。

问题: 在显示数据期间,请让我知道我在哪里出错。

谢谢。

3 个答案:

答案 0 :(得分:1)

我首先要修复您的数据类型,时间应该存储为time,而不是varchar。然后,一旦修复了数据类型,就可以将数据视为(一次)数据。

USE Sandbox;
GO

CREATE TABLE dbo.time_test ([type] varchar(10),
                            [time] time);
INSERT INTO dbo.time_test
VALUES ('A', '01:25'); --Assumes hh:mm
INSERT INTO dbo.time_test
VALUES ('B', '02:30');
INSERT INTO dbo.time_test
VALUES ('C', '05:56');
INSERT INTO dbo.time_test
VALUES ('D', '00:50');
GO

SELECT ISNULL([type],'Total') AS [Type],
       DATEADD(MINUTE,SUM(DATEDIFF(MINUTE,'00:00',[time])),CONVERT(time(0),'00:00')) AS [Time]
FROM dbo.time_test
GROUP BY [type] WITH ROLLUP;

GO

DROP TABLE dbo.time_test;

这将返回:

Type       Time
---------- ----------------
A          01:25:00
B          02:30:00
C          05:56:00
D          00:50:00
Total      10:41:00

答案 1 :(得分:0)

我确实有类似的要求,并这样解决了它:

select type, RTRIM(time/60) + ':' + RIGHT('0' + RTRIM(time%60),2) from
( 
    select type= ISNULL(type, 'Total'),
      time= SUM(DATEDIFF(MINUTE, '0:00:00', time))
    from time_test
    GROUP BY ROLLUP(type)
) x

我相信这是更容易理解且更容易追踪的

type    time
A       1:25
B       2:30
C       5:56
D       0:50
Total   10:41

编辑:更新了hour can be more then 24 hours

的查询
select type, RTRIM(time/60) + ':' + RIGHT('0' + RTRIM(time%60),2) from
( 
    select type= ISNULL(type, 'Total'),
      time= SUM(DATEDIFF(MINUTE, '0:00:00', 
        DATEADD(day, SUBSTRING(time,0,CHARINDEX(':',time,0)) / 24,
        DATEADD(hour, SUBSTRING(time,0,CHARINDEX(':',time,0)) % 24, 
        DATEADD(minute,SUBSTRING(time,CHARINDEX(':',time)+1,LEN(time)) +0, 0)) )))
    from time_test
    GROUP BY ROLLUP(type)
) x

示例结果:

A       1:25
B       2:30
C       5:56
D       70:50
Total   80:41

答案 2 :(得分:0)

好的代码复查很快,请尝试避免使用VARCHAR数据类型来存储日期和/或时间。

以下是我所做的:

create table time_test (type varchar(10),time1 time)
insert into time_test values ('A','00:01:30')
insert into time_test values ('B','00:02:30')
insert into time_test values ('C','00:01:00')
insert into time_test values ('D','00:02:30')

SELECT
  type = ISNULL(type, 'Total'),
  seconds_worked = SUM(DATEDIFF(SECOND, '00:00', time1))
  FROM time_test
GROUP BY ROLLUP(type);

您将看到总计为450,如果您从提供的数据中手动计算得出的结果是正确的,即总时间为450秒。

您可以使用以下公式以分钟和秒为单位返回时间:

RTRIM(**450**/60) + ':' + RIGHT('0' + RTRIM(**450**%60),2)