在SQL Server中向行添加时间

时间:2018-02-06 19:55:12

标签: sql-server sum

我正在尝试在SQL Server中添加重复行的值。这就是数据的外观

 SESSION ID  |TALK TIME|TSEC|TMIN |
 ------------+---------+----+-----+
 34000018959 |00:34.0  |34  |0.56 |
 34000018959 |20:29.0  |1229|20.48|

这是我用来添加整数

的查询
SELECT 
    [SESSION ID], 
    SUM([TALK TIME]) AS [TALK TIME], 
    SUM([TSEC]) AS [TSEC], 
    SUM([TMIN]) AS [TMIN]
FROM   
    [TABLE1]
GROUP BY 
    [SESSION ID], [TALK TIME]

没有使用这个我能够总结TSECTMIN我想要的。但无法获得SUM的{​​{1}}。

这是我希望看到的输出:

Talk Time

1 个答案:

答案 0 :(得分:3)

一种简单的方法是将秒数相加并得出其余部分

示例

Declare @T table ([SESSION ID] varchar(50),[TALK TIME] varchar(50),tSec int,tmin decimal(10,2))
Insert Into @T values 
('34000018959','00:34.0',34,0.56),
('34000018959','20:29.0',1229,20.48)

Select [Session ID]
      ,[Talk Time] = format(dateadd(SECOND,sum(tSec),0),'mm:ss.f')
      ,TSec = sum(tSec)
      ,TMin = cast(sum(tSec+0.0)/60 as decimal(10,2))
 From  @T
 Group By [Session ID]

<强>返回

Session ID  Talk Time   TSec    TMin
34000018959 21:03.0     1263    21.05