如何在SQL里进行有条件的重复记录查询并统计总数的

jousca 2007-10-06 06:04:32
目前是这样一个情况。
表格式如下:

服务器地址 登录用户号
192.168.0.1 10001
192.168.0.1 10001
192.168.0.2 10001
192.168.0.2 10001
192.168.0.2 10001
192.168.0.2 10001
192.168.0.2 10001
192.168.0.2 10001
192.168.0.2 10001
192.168.0.2 10001
192.168.0.2 10002
192.168.0.2 10002
192.168.0.3 10002
192.168.0.3 null
192.168.0.3 null
192.168.0.3 10002
…… (还有很多条)

要求是:
1.统计并显示用户登录出现次数排名。
2.登录相同的服务器最多只计算5次,多的登录记录无效。
3.忽略掉“登录用户号”是null(空白)的记录。

预期出现的结果是:

排名 登录用户号 有效次数
1 10001 7 (虽然记录有10条,但因上面第二条规则缘故,有4条记录忽略)
2 10002 4
……


这个问题对我这菜鸟实在是太难了。我会统计总记录中,每个用户号出现多少次和简单按出现次数排名,但是不会统计“相同的服务器最多只计算5次,多的登录记录无效”这种前提条件的的。求教于各位前辈,请问应该如何解决?


...全文
323 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinawzcbn 2007-10-06
  • 打赏
  • 举报
回复
学习了
Limpire 2007-10-06
  • 打赏
  • 举报
回复
方法2
--原始数据:@T
declare @T table(服务器地址 varchar(11), 登录用户号 int)
insert @T
select '192.168.0.1',10001 union all
select '192.168.0.1',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10002 union all
select '192.168.0.2',10002 union all
select '192.168.0.3',10002 union all
select '192.168.0.3',null union all
select '192.168.0.3',null union all
select '192.168.0.3',10002

--如果原始数据表有主键或唯一性列,不需要临时表:
select ID=identity(int,1,1),* into #Temp from @T where 登录用户号 is not null

select 登录用户号,有效次数=count(*)
from #Temp a where ID in (select top 5 ID from #Temp where 服务器地址=a.服务器地址 and 登录用户号=a.登录用户号)
group by 登录用户号
order by 有效次数 desc
/*
登录用户号 有效次数
10001 7
10002 4
*/

--删除测试
drop table #Temp
Limpire 2007-10-06
  • 打赏
  • 举报
回复
--原始数据:@T
declare @T table(服务器地址 varchar(11), 登录用户号 int)
insert @T
select '192.168.0.1',10001 union all
select '192.168.0.1',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10001 union all
select '192.168.0.2',10002 union all
select '192.168.0.2',10002 union all
select '192.168.0.3',10002 union all
select '192.168.0.3',null union all
select '192.168.0.3',null union all
select '192.168.0.3',10002

select 登录用户号,有效次数=sum(case when IP>5 then 5 else IP end)
from (select 登录用户号,服务器地址,IP=count(1) from @T where 登录用户号 is not null group by 登录用户号,服务器地址) AS a
group by 登录用户号
order by 有效次数 desc
/*
登录用户号 有效次数
10001 7
10002 4
*/

select 排名=identity(int,1,1),登录用户号,有效次数=sum(case when IP>5 then 5 else IP end)
into #Result
from (select 登录用户号,服务器地址,IP=count(1) from @T where 登录用户号 is not null group by 登录用户号,服务器地址) AS a
group by 登录用户号
order by 有效次数 desc

select * from #Result
/*
排名 登录用户号 有效次数
1 10001 7
2 10002 4
*/

--删除测试
drop table #Result
Limpire 2007-10-06
  • 打赏
  • 举报
回复
select 登录用户号,有效次数=sum(case when IP>5 then 5 else IP end)
from (select 登录用户号,服务器地址,IP=count(1) from 表 where 登录用户号 is not null group by 登录用户号,服务器地址) AS a
group by 登录用户号
order by 有效次数 desc
jousca 2007-10-06
  • 打赏
  • 举报
回复
非常感谢Limpire的解答,我参考了您在2楼给出的方式,完美解决了这个问题。再次感谢。

再追加一个小问题:这论坛加分怎么加?:) 我点加分它说发帖还不够5天…… :P

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧