选择每小时连接数最多的用户

时间:2014-04-23 18:16:36

标签: sql sql-server subquery max

我将IIS日志导入SQL Server。

表架构:

CREATE TABLE IIS_Log (
    [cdate] [varchar] (255) NULL,
    [ctime] [varchar] (255) NULL,
    [username] [VARCHAR] (16) NULL
    )

IIS_Log中的每一行都显示用户与服务器之间的连接,如下所示:

username | cdate      | ctime
---------+------------+------
Andy     | 2014-01-01 | 07:10
Andy     | 2014-01-01 | 07:13
Andy     | 2014-01-01 | 07:45
Andy     | 2014-01-01 | 07:05

Andy     | 2014-01-01 | 09:30
Andy     | 2014-01-01 | 09:13

Andy     | 2014-01-01 | 17:45

John     | 2014-01-01 | 12:05
John     | 2014-01-01 | 12:05
John     | 2014-01-01 | 12:05
John     | 2014-01-01 | 12:25
John     | 2014-01-01 | 12:32

John     | 2014-01-01 | 13:00   -- These connections should not
John     | 2014-01-01 | 13:09   -- come in the result set because a heavier
John     | 2014-01-01 | 13:15   -- connection exists for John (with 5 connections)
John     | 2014-01-01 | 13:18   --

John     | 2014-01-01 | 10:10
John     | 2014-01-01 | 10:12

John     | 2014-01-01 | 07:10

David    | 2014-01-01 | 05:20

我希望看到至少有3位用户的每小时总连接次数

请注意,每个用户只需要一行。在这个例子中,约翰不应该来两次。

所以示例的结果集是这样的:

username | connections | cdate      | ctime
---------+-------------+------------+------
Andy     | 4           | 2014-01-01 | 07
John     | 5           | 2014-01-01 | 12

2 个答案:

答案 0 :(得分:3)

;with x as (
    select username, cdate, left(ctime,2) as hr, count(*) as cnt
    from iis_log
    group by username, cdate, left(ctime,2)
),
y as (
    select *, row_number() over(partition by username, cdate order by cnt desc) as rn
    from x
)
select *
from y
where rn = 1
and cnt >= 3

答案 1 :(得分:2)

我的回答就像院长一样。但我不使用CTE。我也试图在没有partition by -

的情况下这样做
select *
from 
(
select 
ROW_NUMBER() over (
    partition by username order by username asc, 
    count(username) desc
) as rowid,
username, 
cdate, 
LEFT(ctime,2) as ctime, 
COUNT(username) as cnt
from IIS_Log
group by username, cdate, LEFT(ctime,2)
having COUNT(username) >= 3
) as src 
where rowid = 1