SQL语句问题,详情请进

KcSoft 2007-07-08 11:06:33
LeiXing value xingming
1001 90 学生1
1001 30 学生2
1001 80 学生3
1001 52 学生4
1002 50 学生5
1002 62 学生6
1002 60 学生7
1002 30 学生8
1002 50 学生9

用语句得到LeiXing 相同的前三位的xingming 和value
1001 90 学生1
1001 80 学生3
1001 52 学生4
1002 62 学生6
1002 60 学生7
1002 50 学生5
1002 50 学生9

...全文
200 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
winjay84 2007-07-08
  • 打赏
  • 举报
回复
你好:hellowork(一两清风)

----方法1:
select * from @t as a where (select count(*) from @t where LeiXing = a.LeiXing and value > a.value) < 3
----方法2:
select * from @t as a where not exists(select 1 from @t where LeiXing = a.LeiXing and value > a.value group by LeiXing having count(*) > 2)

你的方法我不是很理解。
首先 方法1 爲什麽是<3 而不是<=3?
其次 方法2 不太明白
請分析下好嗎?謝謝!

hellowork 2007-07-08
  • 打赏
  • 举报
回复
试试其它方法:
declare @t table(LeiXing varchar(10), value int, xingming varchar(10))
insert @t
select '1001', 90, '学生1' union all
select '1001', 30, '学生2' union all
select '1001', 80, '学生3' union all
select '1001', 52, '学生4' union all
select '1002', 50, '学生5' union all
select '1002', 62, '学生6' union all
select '1002', 60, '学生7' union all
select '1002', 30, '学生8' union all
select '1002', 50, '学生9'

----方法1:
select * from @t as a where (select count(*) from @t where LeiXing = a.LeiXing and value > a.value) < 3
----方法2:
select * from @t as a where not exists(select 1 from @t where LeiXing = a.LeiXing and value > a.value group by LeiXing having count(*) > 2)

/*结果
LeiXing value xingming
---------- ----------- ----------
1001 90 学生1
1001 80 学生3
1001 52 学生4
1002 50 学生5
1002 62 学生6
1002 60 学生7
1002 50 学生9
*/
bill024 2007-07-08
  • 打赏
  • 举报
回复
改下
select * from test a where value in
(
select top 3 value from test where LeiXing=a.LeiXing order by value desc
)
order by LeiXing,value desc,xingming

LeiXing value xingming
----------- ----------- --------------------
1001 90 学生1
1001 80 学生3
1001 52 学生4
1002 62 学生6
1002 60 学生7
1002 50 学生5
1002 50 学生9

(所影响的行数为 7 行)
bill024 2007-07-08
  • 打赏
  • 举报
回复
LeiXing value xingming
----------- ----------- --------------------
1001 90 学生1
1001 80 学生3
1001 52 学生4
1002 50 学生5
1002 62 学生6
1002 60 学生7
1002 50 学生9

(所影响的行数为 7 行)
bill024 2007-07-08
  • 打赏
  • 举报
回复
create table test(LeiXing int,value int,xingming varchar(20))
insert test select 1001,90,'学生1'
union all select 1001,30,'学生2'
union all select 1001,80,'学生3'
union all select 1001,52,'学生4'
union all select 1002,50,'学生5'
union all select 1002,62,'学生6'
union all select 1002,60,'学生7'
union all select 1002,30,'学生8'
union all select 1002,50,'学生9'


select * from test a where value in
(
select top 3 value from test where LeiXing=a.LeiXing order by value desc
)
winjay84 2007-07-08
  • 打赏
  • 举报
回复
謝謝大家,我明白了!
xiaoleige00 2007-07-08
  • 打赏
  • 举报
回复
新建的SQL群 19078538 欢迎大家加入,共同研究,一起进步!
lijie28 2007-07-08
  • 打赏
  • 举报
回复
方法1使用的<3就是count()值等于0,1,2
方法2使用的不大于2,也是count()值等于0,1,2.
可以这样查看一下:
declare @t table(LeiXing varchar(10), value int, xingming varchar(10))
insert @t
select '1001', 90, '学生1' union all
select '1001', 30, '学生2' union all
select '1001', 80, '学生3' union all
select '1001', 52, '学生4' union all
select '1002', 50, '学生5' union all
select '1002', 62, '学生6' union all
select '1002', 60, '学生7' union all
select '1002', 30, '学生8' union all
select '1002', 50, '学生9'

----方法1的count值
select *,(select count(*) from @t where LeiXing = a.LeiXing and value > a.value )
from @t as a
----方法2的count值
select *,isnull((select count(*) from @t where LeiXing = a.LeiXing and value > a.value group by LeiXing ),0)
from @t as a

/*结果
LeiXing value xingming
---------- ----------- ---------- -----------
1001 90 学生1 0
1001 30 学生2 3
1001 80 学生3 1
1001 52 学生4 2
1002 50 学生5 2
1002 62 学生6 0
1002 60 学生7 1
1002 30 学生8 4
1002 50 学生9 2
*/

binglengdexin2 2007-07-08
  • 打赏
  • 举报
回复
create table test(leixing int,value int,xingming varchar(50))
insert test select '1001',90,'学生1'
union all select '1001',30,'学生2'
union all select '1001',80,'学生3'
union all select '1001',52,'学生4'
union all select '1002',50,'学生5'
union all select '1002',62,'学生6'
union all select '1002',60,'学生7'
union all select '1002',30,'学生8'
union all select '1002',50,'学生9'
go

select * from test where value>(select min(value) value from test) order by leixing,value desc

drop table test
go
/----------------------------/结果
1001 90 学生1
1001 80 学生3
1001 52 学生4
1002 62 学生6
1002 60 学生7
1002 50 学生9
1002 50 学生5
hellowork 2007-07-08
  • 打赏
  • 举报
回复
方法1使用的<3就是count()值等于0,1,2
方法2使用的不大于2,也是count()值等于0,1,2.
可以这样查看一下:
declare @t table(LeiXing varchar(10), value int, xingming varchar(10))
insert @t
select '1001', 90, '学生1' union all
select '1001', 30, '学生2' union all
select '1001', 80, '学生3' union all
select '1001', 52, '学生4' union all
select '1002', 50, '学生5' union all
select '1002', 62, '学生6' union all
select '1002', 60, '学生7' union all
select '1002', 30, '学生8' union all
select '1002', 50, '学生9'

----方法1的count值
select *,(select count(*) from @t where LeiXing = a.LeiXing and value > a.value )
from @t as a
----方法2的count值
select *,isnull((select count(*) from @t where LeiXing = a.LeiXing and value > a.value group by LeiXing ),0)
from @t as a

/*结果
LeiXing value xingming
---------- ----------- ---------- -----------
1001 90 学生1 0
1001 30 学生2 3
1001 80 学生3 1
1001 52 学生4 2
1002 50 学生5 2
1002 62 学生6 0
1002 60 学生7 1
1002 30 学生8 4
1002 50 学生9 2
*/
中国风 2007-07-08
  • 打赏
  • 举报
回复
hellowork(一两清风) ( )
写了两种常用的写法.
都是用count(1)记录数来限制
< 3包含0\2\3记录为0时为当前最大值

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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