📄 subject_61148.htm
字号:
<p>
序号:61148 发表者:little_friend 发表日期:2003-11-18 09:44:16
<br>主题:请问一个关于排序的问题
<br>内容:现在有一个学生成绩表<BR>——————<BR>姓名 成绩<BR>——————<BR>徐志慧 90<BR>郭徵 80<BR>牛英俊 80<BR>朱小鹏 70<BR>雷波 70<BR>徐景 75<BR>徐志 75<BR><BR>要增加一个“排名”列<BR>——————<BR>姓名 成绩 排名<BR>————————<BR>徐志慧 90 1<BR>郭徵 80 2<BR>牛英俊 80 2<BR>朱小鹏 70 4<BR>雷波 70 4<BR>徐景 75 6<BR>徐志 75 6<BR>例如并列第二 的排名都为2,其后顺延,没有第三名,<BR>请问应该用什么操作<BR><BR><BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:邓瑞涛 回复日期:2003-11-19 03:10:56
<br>内容:CREATE TABLE [dbo].[a] (<BR> [price] [int] NULL ,<BR> [priceid] [int] IDENTITY (1, 1) NOT NULL ,<BR> [xm] [varchar] (10) NULL ,<BR> [pm] [int] NULL <BR>) ON [PRIMARY]<BR><BR>insert a (xm , price ) values ('徐志慧' , 90 )<BR><BR>insert a (xm , price ) values ('郭徵' , 80)<BR><BR>insert a (xm,price ) values ('牛英俊', 80)<BR><BR>insert a (xm,price ) values ('朱小鹏', 70)<BR><BR>insert a (xm,price ) values ('雷波', 70)<BR><BR>insert a (xm,price ) values ('徐景', 75)<BR><BR>insert a (xm,price ) values ('徐志', 75)<BR><BR><BR>/*声明变量*/<BR>declare @pm int , @price_prev int , @i int<BR>declare @priceid int , @price int <BR><BR><BR>/*声明游标*/<BR>declare temp_cur cursor<BR> for select priceid, price<BR> from a<BR> order by price desc<BR><BR><BR>/*打开游标*/<BR>open temp_cur<BR><BR>/*从游标指定行取值*/<BR>fetch next from temp_cur into @priceid, @price<BR><BR>select @price_prev = @price<BR>select @pm=1 , @i=1<BR><BR>/*判断是否到尾*/<BR>while @@fetch_status=0 <BR>begin<BR> if @price = @price_prev <BR> update a set pm = @pm where priceid = @priceid<BR> else<BR> begin <BR> update a set pm = @i where priceid = @priceid <BR> select @pm = @i <BR> end <BR> <BR> select @price_prev = @price <BR> select @i = @i+1<BR> /*游标继续向下移动*/<BR> fetch next from temp_cur into @priceid, @price<BR>end<BR>/*关闭游标*/<BR>deallocate temp_cur<BR><BR><BR>select xm ,price , pm from a order by price desc<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -