首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

mm馬上送分取最大值問題?解决办法

2012-03-23 
mm馬上送分取最大值問題?表studentSTUDENTID姓名分數1A922A943B984B995C98以姓名分組要取出分數最大值的所

mm馬上送分取最大值問題?
表student  

STUDENTID   姓名   分數      
1                     A           92
2                     A           94
3                     B           98
4                     B           99
5                     C           98

以姓名分組要取出分數最大值的所有記錄!


[解决办法]
select *
from 表名 AS t
where 分數 = (select max(分數) from 表名 where 姓名=T.姓名)
[解决办法]
select a.* from tb a,
(select 姓名, max(分數) as 分数 group by 姓名) b
where a.姓名=b.姓名 and a.分数=b.分数
[解决办法]
declare @tab table (id int, 姓名 varchar(100), 分数 int)
insert into @tab
select 1, 'A ',92 union all
select 2, 'A ',94 union all
select 3, 'B ',98 union all
select 4, 'B ',99 union all
select 5, 'C ',98

Select * From @tab t Where 分数 In (Select max(分数) From @tab b Where b.姓名 = t.姓名)

(所影响的行数为 5 行)

id 姓名 分数
----------- ---------------------------------------------------------------- -----------
5 C 98
4 B 99
2 A 94

热点排行