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
from (select 登录用户号,服务器地址,IP=count(1) from 表 where 登录用户号 is not null group by 登录用户号,服务器地址) AS a
group by 登录用户号
order by 有效次数 desc
解决方案 »
- 求一个讲解sql基础的书
- 温柔一问
- 求一sql语句 高手帮忙!!!50分送上~~~
- 好久没上来啦!
- 安装SQL2008发生的错误
- nvarchar字段 如 SQL_Latin1_General_CP1_CI_AS 的排序规则是什么意思?无可用分了。大家发扬一下风格吧 :)
- 触发器为何不触发?很奇怪。
- 这个触发器好像没错啊,可是为什么不能正常运作呢?
- 高手请进:如何写一个程序将EXCEL表自动导入SQL SERVER数据库中??????????
- 如何在 SQL SERVER 用变量作表名 insert @table_name
- sql 不存在或拒绝访问~
- 安装了sql server2005,有一个进程"reportingserviceservice.exe"占用66占内存,可以强行中止吗?
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',10002select 登录用户号,有效次数=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 有效次数 descselect * from #Result
/*
排名 登录用户号 有效次数
1 10001 7
2 10002 4
*/--删除测试
drop table #Result
--原始数据:@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 nullselect 登录用户号,有效次数=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