时间范围内的不同值的计数

时间:2013-02-17 00:43:55

标签: sql sql-server select count

我有一张这样的表

Time   |  user_id
12100  |    23
12100  |    23
12100  |    22
12100  |    19
12100  |    20
...
12160  |    273
12160  |    223
12160  |    1223
...

时间以秒为单位。我正在寻找一种方法来在一分钟内获得唯一的user_id计数,并让结果表看起来像这样

Minute  |  Count
  1     |  36
  2     |  100
...

假设它从12100开始,那么分钟1来自12100 ~ 12159,分钟2来自12160 to 12219

非常感谢帮助。

更新:(这是我尝试的,但似乎包括重复计数)

SELECT ROW_NUMBER() OVER (ORDER BY [Time]) AS [Row]
        , [Time]
        , COUNT(DISTINCT([user_id])) as [Count]
    INTO [table].[dbo].[temp]
  FROM [db].[dbo].[table]
  GROUP BY [Time]

  SELECT t.[Minute], SUM(t.[Count]) AS [Count]
    FROM
    (SELECT (ROW_NUMBER() OVER (ORDER BY [Row]) + 59)/60 AS [Minute]
            , SUM([Count]) AS [Count]
        FROM [db].[dbo].[temp]
        GROUP BY [Row]) AS t
    GROUP BY t.[Minute]
    ORDER BY t.[Minute]

DROP TABLE [db].[dbo].[temp]

1 个答案:

答案 0 :(得分:3)

创建一个表达式,将时间转换为目标分钟数并按以下方式分组:

select
    ((Time - 12000) / 60) + 1 as Minute,
    count(distinct user_id) as Count
from ... 
group by ((Time - 12000) / 60) + 1