sql查詢每個班上成績最高的學生資訊
資料庫表和資料準備:
if exists (select * from sysobjects where id = object_id('[classinfo]') and objectproperty(id, 'isusertable') = 1)查詢每班最高分數的sql語句:drop table [classinfo]
create table [classinfo] (
[id] [bigint] not null,
[classid] [bigint] not null,
[classname] [nvarchar] (50) not null,
[stat] [varchar] (2) not null default (1),
[autoid] [bigint] identity (1, 1) not null)
alter table [classinfo] with nocheck add constraint [pk_classinfo] primary key nonclustered ( [id] )
set identity_insert [classinfo] on
insert [classinfo] ([id],[classid],[classname],[stat],[autoid]) values ( 1,1,n'計算機一班',n'1',1)
insert [classinfo] ([id],[classid],[classname],[stat],[autoid]) values ( 2,2,n'計算機二班',n'1',2)
insert [classinfo] ([id],[classid],[classname],[stat],[autoid]) values ( 4,3,n'計算機三班',n'0',3)
insert [classinfo] ([id],[classid],[classname],[stat],[autoid]) values ( 5,4,n'計算機四班',n'1',4)
insert [classinfo] ([id],[classid],[classname],[stat],[autoid]) values ( 6,5,n'計算機五班',n'1',6)
set identity_insert [classinfo] off
if exists (select * from sysobjects where id = object_id('[stuinfo]') and objectproperty(id, 'isusertable') = 1)
drop table [stuinfo]
create table [stuinfo] (
[id] [bigint] identity (1, 1) not null,
[username] [nvarchar] (50) null,
[userpwd] [nvarchar] (50) null,
[classid] [bigint] null,
[score] [numeric] (10,2) null,
[age] [int] null,
[createtime] [datetime] null default (getdate()))
alter table [stuinfo] with nocheck add constraint [pk_stuinfo] primary key nonclustered ( [id] )
set identity_insert [stuinfo] on
insert [stuinfo] ([id],[username],[userpwd],[classid],[score],[age],[createtime]) values ( 1,n'001',n'0004',1,123.22,25,n'2012/8/24 10:58:10')
insert [stuinfo] ([id],[username],[userpwd],[classid],[score],[age],[createtime]) values ( 2,n'002',n'154',2,888.00,21,n'2012/8/24 10:58:10')
insert [stuinfo] ([id],[username],[userpwd],[classid],[score],[age],[createtime]) values ( 3,n'003',n'555',1,888.00,16,n'2012/8/24 10:58:10')
insert [stuinfo] ([id],[username],[userpwd],[classid],[score],[age],[createtime]) values ( 4,n'004',n'644',2,85.60,18,n'2012/8/24 10:58:10')
insert [stuinfo] ([id],[username],[userpwd],[classid],[score],[age],[createtime]) values ( 5,n'pkm',n'123',3,46.00,19,n'2012/8/24 10:58:10')
insert [stuinfo] ([id],[username],[userpwd],[classid],[score],[age],[createtime]) values ( 6,n'pkm001',n'123',3,45.56,19,n'2012/8/24 10:58:10')
insert [stuinfo] ([id],[username],[classid],[createtime]) values ( 7,n'2012pkm1',1,n'2012/8/24 10:58:10')
set identity_insert [stuinfo] off
select distinct si.id,si.username,si.score,si.classid,ci.classname原始資料:from stuinfo as si
inner join
(select max(score) as score ,classid from stuinfo
group by classid
)as c1
on c1.score = si.score
inner join classinfo as ci
on ci.classid = si.classid
order by si.classid
查詢結果:
附:刪除重複username記錄,只保留最小的id
delete from stuinfowhere id not in
(select si.id
from stuinfo as si
inner join
( select min(id) as id ,username from stuinfo
group by username
) as c1
on c1.id = si.id
)---或者
delete from stuinfo where id not in
(select min(id) from stuinfo group by username)
SQL查詢每個站點消費金額最高的人
select order takestation,order name m.maxtotal,order total from select order takestation,max order total as maxtotal from order group by order takesta...
sql查詢每門課程成績最高的學生
給出資料庫 sco 如下圖 查出每門課程成績最高的學生 1 select b.id,b.kemu,b.name,b.chengji 2from select kemu,max chengji maxc 3from sco group bykemu a,sco b 4where a.kemu b.ke...
選出每個學生的最大成績的sql語句
name score1 score2 score3 a 70 80 90 b 85 75 95 c 69 74 58 d 54 36 21 如上,學生a,b,c,d分別有3科的成績,現在想查出每個學生的最高分 name maxscore a 90 b 95 c 74 d 54 遇到這種列變行的當然就...