注册 登录
编程论坛 SQL Server论坛

group by将查询结果分组问题(在线等高手)

雪雨星风 发布于 2008-07-05 10:15, 2577 次点击
select distinct top 40 用户呢称,count(*) as 发贴量 from 发贴表
group by 用户呢称
order by count(*) desc
这个查询把所有用户的发贴量查出来了  
查询结果
用户名     发贴量
   sa          100
   admin     80

但是我需要41-80之间的数据
按道理应该这样
select distinct 用户名,count(*) as 发贴量 from 用户表
where 用户名 not in
(
select distinct top 40 用户名,count(*) from 用户表     ---这个查询我只想要按发贴量排过序的用户名
group by 用户名
order by count(*) desc

)
group by 用户名
order by count(*) desc

高手请指点
29 回复
#2
雪雨星风2008-07-05 10:23
实现这种效果有其他好方法吗
#3
雪雨星风2008-07-05 10:44
感觉我没说明白的或没理解的说一声
#4
tomtory2008-07-05 10:45
去41到80的数据应该是在第2页的 哪么就是40*1

select top 40 distinct 用户名,count(*) as 发贴量 from 用户表
where 用户名 not in
(
select distinct top 40 用户名,count(*) from 用户表     ---这个查询我只想要按发贴量排过序的用户名
group by 用户名
order by count(*) desc
)
group by 用户名
order by count(*) desc
#5
tomtory2008-07-05 10:46
意思是每页40条记录
从第40的一条记录开始取
#6
tomtory2008-07-05 10:47
错了 distinct 在前面
#7
雪雨星风2008-07-05 10:54
问题是这个语句查出来的是两列
用户名    发贴量
他不能做为 where条件
select distinct top 40 用户名,count(*) from 用户表     ---这个查询我只想要按发贴量排过序的用户名
group by 用户名
order by count(*) desc
#8
雪雨星风2008-07-05 10:55
where 用户名 not in
(
select distinct top 40 用户名,count(*) from 用户表     ---这个查询我只想要按发贴量排过序的用户名
group by 用户名
order by count(*) desc
)
他的条件若是来自两个的话就完了   肯定错误
#9
雪雨星风2008-07-05 10:57
从41-80条数据的条件还必须不等于按照发贴量排过序的所有用户的前40位
#10
tomtory2008-07-05 10:58
是不是两列的哦一样的噻

你在程序里面不取它不就可以了吗!

怎么非要只要一列的哦 !!
#11
tomtory2008-07-05 11:01
[bo][un]雪雨星风[/un] 在 2008-7-5 10:57 的发言:[/bo]

从41-80条数据的条件还必须不等于按照发贴量排过序的所有用户的前40位



你的意思是这41-80的数据就是数据库表中没有排过序的40-80条噻
#12
tomtory2008-07-05 11:12
select 发帖人 from 发帖表 where [id] in(
select distinct top 5 [id] from 发帖表
where [id] not in (select top 20 [id] from 发帖表)) order by [id]

你这样试试
#13
tomtory2008-07-05 11:16
你的需求就是
先取出41-80之间的人
然后在更具他的发帖量排序
然后只要发帖人这一列??
#14
雪雨星风2008-07-05 11:30
select 发帖人 from 发帖表 where [id] in(
select distinct top 5 [id] from 发帖表
where [id] not in (select top 20 [id] from 发帖表)) order by [id]

你这样试试


发贴表中没ID这个字段啊
#15
雪雨星风2008-07-05 11:33
那个ID是group by 生成的字段
#16
tomtory2008-07-05 11:34
我晕  你把id换成你相应的字段就可以了啊!
#17
tomtory2008-07-05 11:35
不过你怎么不设ID这个字段哦!
加这个字段然后根据这个字段分页多好的哦!!我晕
这个字段为自动增长的噻
#18
tomtory2008-07-05 11:39
select top 5 用户名 from 用户表 where 用户名 not in (select distinct top 5 用户名 from 用户表) group by 用户名 order by 发帖量 desc

在父语句中不要用distinct
#19
tomtory2008-07-05 11:39
发帖量为count(*) 数出来的 就是
select top 5 用户名 from 用户表 where 用户名 not in (select distinct top 5 用户名 from 用户表) group by 用户名 order by count(*) desc
#20
tomtory2008-07-05 11:40
现在你试试  看看查出来的数据是不是你要的数据
#21
雪雨星风2008-07-05 12:02
给点思路啊
#22
雪雨星风2008-07-05 12:04
这我知道  
我需要条件是根据发贴量排序的前40条 的用户名
#23
雪雨星风2008-07-05 12:05
select distinct top 40 用户名,count(*) from 用户表     ---这个查询我只想要按发贴量排过序的用户名
group by 用户名
order by count(*) desc

用它查如何才能只得到用户名
#24
雪雨星风2008-07-05 12:07
group by
用它的话查出来的是用户名  和发贴量
它们俩没法做条件
#25
雪雨星风2008-07-05 12:09
这个问题OK的话
我想统计每个人的再线时间
怎么做
#26
雪雨星风2008-07-05 12:10
登陆后可以获取到他的登陆时间
他离开的时间怎么办    习惯正常退出的用户我想没多少吧
#27
tomtory2008-07-05 13:07
你用我最后发给你的那条SQL语句试过没有的哦!
#28
雪雨星风2008-07-05 13:48
发帖量为count(*) 数出来的 就是
select top 5 用户名 from 用户表 where 用户名 not in (select distinct top 5 用户名 from 用户表) group by 用户名 order by count(*) desc
你这个where 用户名 not in (select distinct top 5 用户名 from 用户表)条件可以查到用户名
#29
雪雨星风2008-07-05 13:55
太强了   佩服
#30
tomtory2008-07-05 14:02
现在可以了噻
!!
合理应用where条件  子查询

很多查询都可以查出来

你认真想想就可以想到的嘛!
1