求助!!关于数据表中字段 排名的SQL

pengteling 2008-03-04 02:39:05
table stu

姓名 语文 数学 英语 语文排名 数学排号 英语排名
张三 99 100 85
李四 95 80 100
王五 96 45 37
.......

这样的一个表格,现在排名字段数据需要 执行SQL语句后 自动更新
请高手帮忙了

我想过办法,就是先按各科排序 插入临时表,最后更新,不过效率似乎不高
select *,语文排名=identity(int,1,1)
into #tmpData_1
from stu
order by 语文 desc

select *,数学排名=identity(int,1,1)
into #tmpData_2
from stu
order by 数学 desc

.......
有多少个字段插入多少个临时表,最后执行更新
update stu set 语文排名=(select 语文排名 from #tmpData_1 where stuname=stuname)
....

因列表数较多,SQL语句繁杂,大家有没有更好的办法?谢谢了
...全文
2851 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaJiaBing 2008-05-08
  • 打赏
  • 举报
回复

CREATE TABLE [dbo].[stu](
[姓名] [nvarchar](4) NULL,
[语文] [int] NULL,
[数学] [int] NULL,
[英语] [int] NULL,
[语文名次] [int] NULL,
[数学名次] [int] NULL,
[英语名次] [int] NULL
) ON [PRIMARY]


update stu set 语文名次= (select count(*) from stu b where a.语文<b.语文) +1,
数学名次 = (select count(*) from stu c where a.数学<c.数学)+1,
英语名次= (select count(*) from stu d where a.英语<d.英语)+1
from stu a
select * from stu
UltraBejing 2008-05-01
  • 打赏
  • 举报
回复
我也想了解,谢谢LZ.
xiaoliaoyun 2008-03-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 pengteling 的回复:]
xiaoliaoyun

-----------
'rank' 不是可以识别的 函数名。
[/Quote]

rank()是sql 2005里才有的.....
yimeng123 2008-03-04
  • 打赏
  • 举报
回复
重金诚聘C++
http://topic.csdn.net/u/20080227/14/8bce0844-bd15-42f0-9cda-a343d5d6601b.html?seed=2111206245
dawugui 2008-03-04
  • 打赏
  • 举报
回复
如果两人成绩相同,名次结果肯定相同.
当然总名次不一定相同.

select t.姓名,t.语文,t.数学,t.英语,
语文排名 = (select count(1) from stu where 语文 > t.语文) + 1,
数学排名 = (select count(1) from stu where 数学 > t.数学) + 1,
英语排名 = (select count(1) from stu where 英语 > t.英语) + 1,
总排名 = (select count(1) from stu where 语文+数学+英语 > t.语文+t.数学+t,英语) + 1
from stu t
pengteling 2008-03-04
  • 打赏
  • 举报
回复
另外使用 count() 时如果两人成绩相同时,名次结果相同,但每个人名次不能相同时怎么处理?
dawugui 2008-03-04
  • 打赏
  • 举报
回复
table stu

姓名 语文 数学 英语 语文排名 数学排号 英语排名
张三 99 100 85
李四 95 80 100
王五 96 45 37


select t.姓名,t.语文,t.数学,t.英语,
语文排名 = (select count(1) from stu where 语文 < t.语文) + 1,
数学排名 = (select count(1) from stu where 数学 < t.数学) + 1,
英语排名 = (select count(1) from stu where 英语 < t.英语) + 1
from stu t
pengteling 2008-03-04
  • 打赏
  • 举报
回复
xiaoliaoyun

-----------
'rank' 不是可以识别的 函数名。
xiaoliaoyun 2008-03-04
  • 打赏
  • 举报
回复
可以在查询的时候在计算排名

declare @a table (姓名 varchar(10),语文 int,数学 int,英语 int)

insert into @a values ('张三',99,100,85)
insert into @a values ('张三',95,80,100)
insert into @a values ('张三',96,45,37)
select * from @a

select 姓名
,语文
,数学
,英语
,rank() over(order by 语文 desc) as 语文排名
,rank() over(order by 数学 desc) as 数学排号
,rank() over(order by 英语 desc) as 英语排名
from @a
bqb 2008-03-04
  • 打赏
  • 举报
回复


declare @tb table(姓名 varchar(20),语文 int,数学 int,英语 int)
insert into @tb
select '张三', 99, 100, 85 union all
select '李四', 95, 80, 100 union all
select '王五', 96, 45, 37

select *,
(select count(1) from @tb where 语文>=a.语文) 语文排名,
(select count(1) from @tb where 数学>=a.数学) 数学排名,
(select count(1) from @tb where 英语>=a.英语) 英语排名
from @tb a

/*
姓名 语文 数学 英语 语文排名 数学排名 英语排名
------------------------------------------------------------
张三 99 100 85 1 1 2
李四 95 80 100 3 2 1
王五 96 45 37 2 3 3
*/
pt1314917 2008-03-04
  • 打赏
  • 举报
回复

declare @t table(姓名 varchar(10),语文 int,数学 int,英语 int,语文排名 int,数学排名 int,英语排名 int)
insert into @t select '张三',99,100,85,0,0,0
insert into @t select '李四',95,80,100,0,0,0
insert into @t select '王五',96,45,37,0,0,0

update @t
set 语文排名=(select count(1) from @t where 语文>a.语文)+1,
数学排名=(select count(1) from @t where 数学>a.数学)+1,
英语排名=(select count(1) from @t where 英语>a.英语)+1
from @t a

select * from @t
gahade 2008-03-04
  • 打赏
  • 举报
回复

create table stu (姓名 varchar(10),语文 int,数学 int,英语 int,语文排名 int,数学排号 int,英语排名 int)
insert into stu(姓名,语文,数学,英语)
select '张三',99,100,85
union all select '李四',95,80,100
union all select '王五',96,45,37

update stu
set 语文排名=(select count(*) from stu b where a.语文<b.语文)+1,
数学排号=(select count(*) from stu c where a.数学<c.数学)+1,
英语排名=(select count(*) from stu d where a.英语<d.英语)+1
from stu a

select * from stu
/*
姓名 语文 数学 英语 语文排名 数学排号 英语排名
---------- ----------- ----------- ----------- ----------- ----------- -----------
张三 99 100 85 1 1 2
李四 95 80 100 3 2 1
王五 96 45 37 2 3 3

(所影响的行数为 3 行)
*/
playwarcraft 2008-03-04
  • 打赏
  • 举报
回复

create table stu(name varchar(10), chinese int,math int,english int, c_id int,m_id int,e_id int)
insert into stu(name,chinese,math,english) select 'A',99,100,80
insert into stu(name,chinese,math,english) select 'B',95,80,100
insert into stu(name,chinese,math,english) select 'C',96,45,37

update stu
set c_id=1+isnull((select count(*) from stu A where A.chinese>stu.chinese),0),
m_id=1+isnull((select count(*) from stu A where A.math>stu.math),0),
e_id=1+isnull((select count(*) from stu A where A.english>stu.english),0)

select * from stu
/*
A 99 100 80 1 1 2
B 95 80 100 3 2 1
C 96 45 37 2 3 3
*/
drop table stu

playwarcraft 2008-03-04
  • 打赏
  • 举报
回复
Update T
set 语文排名=1+isnull((select count(*) from T A where A.语文>T.语文),0),
数学排号=1+isnull((select count(*) from T A where A.数学>T.数学),0),
英语排名=1+isnull((select count(*) from T A where A.英语>T.英语),0),

27,579

社区成员

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

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