行之间的最小和最大日期 - 分组行

时间:2016-03-02 19:18:29

标签: sql tsql sql-server-2005 datediff

我有一张桌子" A"就像在图像中一样,我需要创建一个视图来将数据分组为表" B"。 有什么建议?我会感激每一项提高我技能的投入 感谢

table

1 个答案:

答案 0 :(得分:0)

这应该这样做:

SELECT [USER],
      [ACTIVITY],
      MIN([HOUR]) AS [START],
      MAX([HOUR]) AS [END],
      RIGHT('0'+CAST(DATEDIFF(SECOND, CAST(MIN([HOUR]) AS TIME), CAST(MAX([HOUR]) AS TIME)) / 3600 AS VARCHAR), 2)+':'+RIGHT('0'+CAST((DATEDIFF(SECOND, CAST(MIN([HOUR]) AS TIME), CAST(MAX([HOUR]) AS TIME)) / 60) % 60 AS VARCHAR), 2) AS [MINUTE]
FROM   TAB_A
GROUP BY [USER],
        [ACTIVITY];