求一条sql语句

claymore1114 2009-08-24 10:28:55
求:存储过程

成绩表
id name score(成绩)


我想得到 某一个用户的成绩排名数

根据id得到 成绩 的排名是多少?



...全文
97 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
claymore1114 2009-08-24
  • 打赏
  • 举报
回复
结贴 结贴
水族杰纶 2009-08-24
  • 打赏
  • 举报
回复
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([id] int,[score] int,[name] nvarchar(4))
insert [TB]
select 1,100,N'张三' union all
select 2,80,N'李四' union all
select 3,80,N'王五' union all
select 4,75,N'杨六' union all
select 5,75,N'方七'

select id=(select count(distinct [score])+1 from TB where T.[score]<[score]),
[score],
[name]
from TB t
/*
id point name
----------- ----------- ----
1 100 张三
2 80 李四
2 80 王五
3 75 杨六
3 75 方七

(5 個資料列受到影響)

*/
黄_瓜 2009-08-24
  • 打赏
  • 举报
回复
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([id] int,[point] int,[name] varchar(4))
insert [TB]
select 1,100,'张三' union all
select 2,80,'李四' union all
select 3,80,'王五' union all
select 4,75,'杨六' union all
select 5,75,'方七'

select id=(select count([point])+1 from TB where T.[point]<[point]),point,name
from TB t
--where ........

/*
id point name
----------- ----------- ----
1 100 张三
2 80 李四
2 80 王五
4 75 杨六
4 75 方七

(5 行受影响)


*/
黄_瓜 2009-08-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fredrickhu 的回复:]
SQL code--> 测试数据:[TB]ifobject_id('[TB]')isnotnulldroptable[TB]createtable[TB]([id]int,[point]int,[name]varchar(4))insert[TB]select1,100,'张三'unionallselect2,80,'李四'unionallselect3,80,'王五'unionallselect¡­
[/Quote]
原来是想要这种结果呀。xiaoF可以考虑当个侦探了
gw6328 2009-08-24
  • 打赏
  • 举报
回复
都比较爽.
gw6328 2009-08-24
  • 打赏
  • 举报
回复
select rn from (select rn=row_number() over(order by score desc),* from tb) where id=@id
学习!
SQL77 2009-08-24
  • 打赏
  • 举报
回复
[code=SQL]
CREATE PROC P(@ID INT)
AS
BEGIN
SELECT ID,(SELECT COUNT(*) FROM TB WHERE score>=T.score)AS NUM INTO #T FROM TB T
SELECT NUM FROM #T WHERE ID=@ID
END

[/code]
ChinaJiaBing 2009-08-24
  • 打赏
  • 举报
回复

select id, score,row_number()over(order by score desc) as 名次 from tb
feixianxxx 2009-08-24
  • 打赏
  • 举报
回复
if object_id('成绩表') is not null 
drop table 成绩表
go
create table 成绩表([id] int ,score int,[name] varchar(4))
insert 成绩表
select 1,100,'张三' union all
select 2,90,'李四' union all
select 3,80,'王五' union all
select 4,85,'杨六' union all
select 5,75,'方七'
go
create proc ck1
@id int
as
begin
declare @paim int
select @paim=rn
from (select rn=ROW_NUMBER() over(order by score desc ),* from 成绩表) k
where Id=@id
select @paim
end
exec ck1 4
/*
-----------
3

*/
--小F-- 2009-08-24
  • 打赏
  • 举报
回复
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
go
create table [TB]([id] int,[point] int,[name] varchar(4))
insert [TB]
select 1,100,'张三' union all
select 2,80,'李四' union all
select 3,80,'王五' union all
select 4,75,'杨六' union all
select 5,75,'方七'

select id=(select count([point])+1 from TB where T.[point]<[point]),point,name
from TB t
where name='张三'

/*id point name
----------- ----------- ----
1 100 张三

(1 行受影响)*/
ChinaJiaBing 2009-08-24
  • 打赏
  • 举报
回复

select id, score,row_number()over(order by score desc) as 名次 from tb where name='性名'
Yang_ 2009-08-24
  • 打赏
  • 举报
回复
select *,rank() over(order by score desc)
from 成绩表
黄_瓜 2009-08-24
  • 打赏
  • 举报
回复
要不就给出数据和你想要的结果吧
--小F-- 2009-08-24
  • 打赏
  • 举报
回复
2楼的例子加上查询条件就可以了
liangCK 2009-08-24
  • 打赏
  • 举报
回复
DECLARE @id int;
SET @id = 10;

SELECT *
FROM (
SELECT id,name,score,
rnk = (SELECT COUNT(*)+1 FROM tb WHERE A.score > score)
FROM tb AS A
) AS T
WHERE id = @id



--2005直接用dense_rank()
csdyyr 2009-08-24
  • 打赏
  • 举报
回复
create proc test
@id int
as
select seq
from (
select *,seq=dense_rank() over (order by score desc)
) t
where id=@id
feixianxxx 2009-08-24
  • 打赏
  • 举报
回复
create proc  c
@id int
as
begin
declare @paim int
select @paim=rn
from (select rn=ROW_NUMBER() over(order by score desc ),* from 成绩表) k
where Id=@id
select @paim
end
--小F-- 2009-08-24
  • 打赏
  • 举报
回复
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([id] int,[point] int,[name] varchar(4))
insert [TB]
select 1,100,'张三' union all
select 2,80,'李四' union all
select 3,80,'王五' union all
select 4,75,'杨六' union all
select 5,75,'方七'

select id=(select count([point])+1 from TB where T.[point]<[point]),point,name
from TB t

/*
id point name
----------- ----------- ----
1 100 张三
2 80 李四
2 80 王五
4 75 杨六
4 75 方七

(5 行受影响)


*/

drop table TB

--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([id] int,[point] int,[name] Nvarchar(4))
insert [TB]
select 1,100,N'张三' union all
select 2,80,N'李四' union all
select 3,80,N'王五' union all
select 4,75,N'杨六' union all
select 5,75,N'方七'

select id=(select count(distinct point) from TB where T.[point]<=[point]),
point,
[name]
from TB t
/*
id point name
----------- ----------- ----
1 100 张三
2 80 李四
2 80 王五
3 75 杨六
3 75 方七

(5 個資料列受到影響)
*/

黄_瓜 2009-08-24
  • 打赏
  • 举报
回复
select id, score from tb where name='张三'

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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