id 数学 英语 语文
1 90 88 30
2 90 70 52
3 50 88 60
4 90 88 40请高手教下我怎么写这个sql语句 我现在要做一个连续的判读 : 我先取数学成绩最好的(如果数学成绩最好的没有重复我就停止取值,直接取出id),如果数学最好的有重复我再取英语成绩最好的(如果英语成绩最好的没有重复我就停止取值取出id) 如果再有重复 我最后比较语文成绩最好的 取出他的id
求高手赐教啊 ……
1 90 88 30
2 90 70 52
3 50 88 60
4 90 88 40请高手教下我怎么写这个sql语句 我现在要做一个连续的判读 : 我先取数学成绩最好的(如果数学成绩最好的没有重复我就停止取值,直接取出id),如果数学最好的有重复我再取英语成绩最好的(如果英语成绩最好的没有重复我就停止取值取出id) 如果再有重复 我最后比较语文成绩最好的 取出他的id
求高手赐教啊 ……
if ...
...
else if ...
...
else
...
对数学成绩降序 获取到数学的最好成绩
对英语成绩降序 获取到英语的最好成绩
对语文成绩降序 获取到语文的最好成绩
select top 1 * from scoretb order by 数学 desc,英语 desc,语文 desc
go
--> -->
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([数学] int,[英语] int,[语文] int)
Insert #T
select 90,88,30 union all
select 90,70,52 union all
select 50,88,60 union all
select 90,88,40
Go
Select ID=DENSE_RANK()OVER(ORDER BY [数学] desc,[英语]desc,[语文]desc),*
from #T
/*
ID 数学 英语 语文
1 90 88 40
2 90 88 30
3 90 70 52
4 50 88 60
*/
create table sc(id int, 数学 int, 英语 int, 语文 int)insert into sc
select 1, 90, 88, 30 union all
select 2, 90, 70, 52 union all
select 3, 50, 88, 60 union all
select 4, 90, 88, 40
declare @mm int,@me int,@mc intselect @mm=max(数学),@me=max(英语),@mc=max(语文) from scif (select count(*) from sc where 数学=@mm)>=2
begin
if (select count(*) from sc where 英语=@me)>=2
begin
if (select count(*) from sc where 语文=@mc)>=2
begin
print null
end
else
begin
select * from sc where 语文=@mc
end
end
else
begin
select * from sc where 英语=@me
end
end
else
begin
select * from sc where 数学=@mm
endid 数学 英语 语文
----------- ----------- ----------- -----------
3 50 88 60