惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
Know Your Adversary
Know Your Adversary
T
Tor Project blog
C
Cisco Blogs
G
GRAHAM CLULEY
S
Schneier on Security
AWS News Blog
AWS News Blog
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
爱范儿
爱范儿
S
SegmentFault 最新的问题
S
Security Affairs
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
J
Java Code Geeks
美团技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
U
Unit 42
Latest news
Latest news
T
Tailwind CSS Blog
GbyAI
GbyAI
月光博客
月光博客
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
Forbes - Security
Forbes - Security
S
Securelist
AI
AI
Recent Announcements
Recent Announcements
C
Check Point Blog
I
Intezer
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
NISL@THU
NISL@THU
Hacker News: Ask HN
Hacker News: Ask HN
O
OpenAI News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
V2EX - 技术
V2EX - 技术
Y
Y Combinator Blog
博客园 - 【当耐特】
aimingoo的专栏
aimingoo的专栏
F
Full Disclosure
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News

博客园 - 无名

sql2005分页SQL 数据库设计原则 sql2005排名函数 如何提高数据库性能,减少数据库服务器压力瓶颈一两个小方法 sql Server 索引优化 Using DeepLoad In NetTiers 看nettiers整理出来的codesmith模板编写要点 - 无名 - 博客园 .Net WinForm学习笔记 在javascript中,怎样响应键盘事件(转 - 无名 - 博客园 各分数段人数统计 DataGrid - 导出Excel文件 DataGrid导出excel和word的例子 DataGrid - 导出Excel文件 利用剪切板将DataSet的数据导出到Excel DataGrid相关知识总结(收集自各位老大处) 使用DES加密解密代码(C# & vb.Net),好东东,拷得,贴一下 dataGrid的手写样式 在DNN中获取所有模块信息 几个DNN里的技巧(转)(真的是好东东啊)
排名问题
无名 · 2006-06-20 · via 博客园 - 无名


有一成绩管理系统,按总分给学生成绩排名,在SQL中如何实现  
faq-it.org/software_project/排名情况有:  
1。允许并列名次,  
2。增删时能相应更改!  
---------------------------------------------------------------  
 
select  (select  count(*)+1  from  table1  where  总分  >  A.总分  )  as  名次  ,  *  
 from  table1  as  A  order  by  名次    
 
---------------------------------------------------------------  
 
select  (select  count(*)+1  from  t  where  分数>=A.分数)  as  '名次',*  from  t  A  
 order  by  分数  desc  

请问怎样编写SQL语句:在成绩列输入数据,然后对其进行排序,并把结果存入排名列.


一个表有两个列:'成绩'与'排名',现在'成绩'列输入数据,然后再调用一个存储过程对其进行排序,并把排序结果存入'排名'列.  
faq-it.org/cvs/---------------------------------------------------------------  
 
CREATE  PROCEDURE  yourproc  
AS  
DECLARE  @cj    float,  
               @tcj  float,  
               @pm    int  
SELECT  @pm  =  1  
SELECT  @tcj  =  MAX(cj)  FROM  yourtable  
UPDATE  yourtable  
     SET  pm=@pm  
 WHERE  cj=@tcj  
DECLARE  cur  SCROLL  CURSOR  
       FOR  SELECT  cj  
                   FROM  yourtable  
                 ORDER  BY  cj  DESC  
OPEN  cur  
FETCH  FIRST  FROM  cur  INTO  @cj  
WHILE  @@fetch_status=0  
BEGIN  
   IF  @cj<>@tcj  
   BEGIN  
       SELECT  @tcj=@cj  
       SELECT  @pm=@pm+1  
       UPDATE  yourtable  
             SET  pm=@pm  
         WHERE  cj=@tcj  
   END  
   FETCH  NEXT  FROM  cur  INTO  @cj  
END  
CLOSE  cur  
DEALLOCATE  cur  
 
GO  
---------------------------------------------------------------  
也找到了一个方法,不需用游标:  
 
假如表名是Score则有:  
 
IF  EXISTS(SELECT  TABLE_NAME  FROM  INFORMATION_SCHEMA.TABLES  
           WHERE  TABLE_NAME  =  'Select_Into_Temp')  
     DROP  TABLE  Select_Into_Temp  
GO  
 
select  成绩,IDENTITY(smallint,  1,  1)  AS  排名  
into  Select_Into_Temp  
from  Score  
order  by  成绩  
GO  
 
delete    
from  Score  
GO  
 
insert  into  Score  
           select  成绩,排名  
           from  Select_Into_Temp  
GO  
 
DROP  TABLE  Select_Into_Temp  
---------------------------------------------------------------  
select  identity  没考虑到同名次的情况!  
 
可以一句写出来:  
 
create  table  #temp  (cj  decimal(10,2),pm  int)  
go    
insert  into  #temp(cj)  values(80)  
insert  into  #temp(cj)  values(70)  
insert  into  #temp(cj)  values(70)  
insert  into  #temp(cj)  values(90)  
insert  into  #temp(cj)  values(60)  
insert  into  #temp(cj)  values(50)  
go  
 
 
update  #temp  set  pm=b.pm  from  #temp  a,(select  distinct  cj,(select  count(*)+1  from  #temp  where  cj>a.cj  )  as  pm  from  #temp  a  )  b  where  a.cj=b.cj  
 
 
select  *  from  #temp  order  by  pm  
drop  table  #temp  
 
cj                      pm                      
------------  -----------    
90.00                1  
80.00                2  
70.00                3  
70.00                3  
60.00                5  
50.00                6  
 
(所影响的行数为  6  行)