select * from TAB a,(select id,max(分数) from Tab group by id) b where a.id=b.id
select * from tb where id in ( select 类别,min(编号) from tb group by 类别 )
select * from tb a join (select 类别,max(分类) as 分数 from tb group by 类别) b on a.类别=b.类别 and a.分数=b.分数
select * from tb t where not exists(select 1 from tb where 编号=t.编号 and 分数>t.分数)编号 类别 名称 分数 1 语文 A 90 3 数学 C 70
select * from tb where 编号 in (select min(编号) from tb group by 类别) ---or: select * from tb t where not exists(select 1 from tb where 类别=t.类别 and 编号<t.编号)
看不懂是按分数,还是按名称,或者各自MAX聚合。
/* 编号 类别 名称 分数 1 语文 A 90 2 语文 B 80 3 数学 C 70输出结果:1 语文 A 90 3 数学 C 70请问各位大大,这个SQL应该如何写的 */ create table #t (编号 int, 类别 varchar(20), 名称 varchar(20), 分数 int) insert into #t select 1,'语文','A',90 union select 2,'语文','B',80 union select 3,'数学','C',70select a.* from #t a join (select 类别,max(分数) 分数 from #t b group by 类别) b on a.类别=b.类别 and a.分数=b.分数编号 类别 名称 分数 1 语文 A 90 3 数学 C 70drop table #t
1 就代表1 意思是,(select 1 from tb where 编号=t.编号 and 分数>t.分数)只要有就会有1 没有就是null。
/*exists 和select 1的用法解释: exist 即存在。 select 1 from table 与 select * from table 作用上来说是没有差别的,都是查看是否有记录,一般是作条件用的。 select 1 from 中的1是一常量,查到的所有行的值都是它,但从效率上来说,1>*,因为不用查字典表。 */
select * from TAB a,(select id,max(分数) from Tab group by id) b where a.id=b.id
from tb
where id in
(
select 类别,min(编号)
from tb
group by 类别
)
select * from tb t where not exists(select 1 from tb where 编号=t.编号 and 分数>t.分数)编号 类别 名称 分数
1 语文 A 90
3 数学 C 70
---or:
select * from tb t where not exists(select 1 from tb where 类别=t.类别 and 编号<t.编号)
编号 类别 名称 分数
1 语文 A 90
2 语文 B 80
3 数学 C 70输出结果:1 语文 A 90
3 数学 C 70请问各位大大,这个SQL应该如何写的
*/
create table #t (编号 int, 类别 varchar(20), 名称 varchar(20), 分数 int)
insert into #t
select 1,'语文','A',90 union
select 2,'语文','B',80 union
select 3,'数学','C',70select a.*
from #t a
join (select 类别,max(分数) 分数 from #t b group by 类别) b on a.类别=b.类别 and a.分数=b.分数编号 类别 名称 分数
1 语文 A 90
3 数学 C 70drop table #t
没有就是null。
/*exists 和select 1的用法解释:
exist 即存在。
select 1 from table
与
select * from table 作用上来说是没有差别的,都是查看是否有记录,一般是作条件用的。
select 1 from 中的1是一常量,查到的所有行的值都是它,但从效率上来说,1>*,因为不用查字典表。
*/