我如何获得所有会议纪要的最终总和?

时间:2015-12-13 21:28:19

标签: sql sql-server

如何使用以下代码获取所有分钟的总和。我觉得我错过了什么。

结果如下:

ActionLog | Mins

2015-06-15 16:04:00 | 3

2015-06-15 16:25:00 | 2

2015-06-15 16:26:00 | 1

--create our table
DECLARE @TableRows TABLE
(
ID TINYINT,
ActionLog SMALLDATETIME
);

--insert some data
INSERT INTO @TableRows
VALUES
(10,'20150615 16:01:00'),
(10,'20150615 16:04:00'),
(10,'20150615 16:23:00'),
(10,'20150615 16:25:00'),
(10,'20150615 16:26:00');

--set up a CTE which we will perform a self join on
WITH ExampleCTE
AS
(SELECT 
      ROW_NUMBER() OVER(ORDER BY ActionLog) AS RowNum
    , ActionLog
FROM @TableRows)

--now query the CTE using the self join to get our result
SELECT 
      t1.ActionLog
    , DATEDIFF(MINUTE,t2.ActionLog,t1.ActionLog) as Mins
FROM ExampleCTE t1
    LEFT JOIN ExampleCTE t2 ON T1.RowNum = T2.RowNum + 1
WHERE
    DATEDIFF(MINUTE,t2.ActionLog,t1.ActionLog) < 10
ORDER BY
    t1.ActionLog

1 个答案:

答案 0 :(得分:1)

根据您的查询查找差异总和:

--now query the CTE using the self join to get our result
SELECT 
      sum(DATEDIFF(MINUTE,t2.ActionLog,t1.ActionLog)) as Mins
FROM ExampleCTE t1
    LEFT JOIN ExampleCTE t2 ON T1.RowNum = T2.RowNum + 1
WHERE
    DATEDIFF(MINUTE,t2.ActionLog,t1.ActionLog) < 10