计算排名的问题

gdp10000 2005-12-15 11:28:54
现在有一个表,比如
updatatime score1 score2 name
2005-12-5 12 99 王
2005-6-9 31 16 王
2005-9-18 99 40 李
2005-12-9 99 20 李
2005-19-9 80 80 钱


我想得到这样的结果,得到一个排名,以score1为第一关键字,score2为第二关键字,score1相同的时候,score2大的成绩更好,如果有重复的名字的,只按他的最好成绩的算,每个人只出现一次。如这样的结果:

2005-12-9 99 40 李
2005-19-9 80 80 钱
2005-6-9 31 16 王
...全文
102 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlmail2005 2005-12-16
  • 打赏
  • 举报
回复
select distinct name, max(score1),max(score2) from t group by name
cen123 2005-12-16
  • 打赏
  • 举报
回复
Create table studentScore
(updatatime DATETIME NOT NULL DEFAULT GETDATE(),
score1 INT NOT NULL,
score2 INT NOT NULL,
name NVARCHAR(50) NOT NULL)
GO

INSERT studentScore SELECT '2005-12-11',100,20,'cen123' union all
select '2005-12-05',12,99,'王' union all
select '2005-06-09',31,16,'王' union all
select '2005-09-18',99,40,'李' union all
select '2005-12-09',99,20,'李' union all
select '2005-12-09',80,80,'钱'

select * from studentScore order by score1 desc,(select max(score1+score2) as score from
studentScore) desc
gdp10000 2005-12-16
  • 打赏
  • 举报
回复
这样写应该是符合我的要求的,我再研究一下
hpym365 2005-12-16
  • 打赏
  • 举报
回复
--测试环境
create table ussn
(updatetime smalldatetime,
score1 int,
score2 int,
name nvarchar(50))
go
insert ussn
select '2005-12-05',12,99,'王' union all
select '2005-06-09',31,16,'王' union all
select '2005-09-18',99,40,'李' union all
select '2005-12-09',99,20,'李' union all
select '2005-12-09',80,80,'钱'
go
--执行语句
select updatetime=(select top 1 updatetime from ussn where name=u.name order by score1 desc,score2 desc ),
score1=(select top 1 score1 from ussn where name=u.name order by score1 desc,score2 desc ),
score2=(select top 1 score2 from ussn where name=u.name order by score1 desc,score2 desc ),
name
from ussn u
group by name
order by score1 desc
--输出结果
2005-09-18 00:00:00 99 40 李
2005-12-09 00:00:00 80 80 钱
2005-06-09 00:00:00 31 16 王

楼主 你那结果是不是错了? 自己好好看看
第一: 你咋出来个 2005-19-9 80 80 钱 ?
我改成 12-09 号了
第二 :你第一行 2005-12-9 99 40 李
日期也看串行了吧
应该是 2005-9-18 99 40 李 吧?
看看我写的行不
浩方软件HFWMS 2005-12-16
  • 打赏
  • 举报
回复
select distinct name, max(score1),max(score2) from t group by name
gdp10000 2005-12-16
  • 打赏
  • 举报
回复
而且这样查询
max(score1),max(score2)
得不到结果,他只是去算最大值,我只想把表中记录排序,重复的人名取最好的成绩算
gdp10000 2005-12-16
  • 打赏
  • 举报
回复
那我怎么得到时间信息?在查询的结果里面我还想得到时间

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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