?? 積分排序解法2.txt
字號:
/*
表A
姓名 科目 分數
---------------------------------
張三 語文 88
張三 數學 90
張三 政治 90
李四 語文 90
李四 數學 92
王五 語文 70
王五 數學 80
劉二 語文 90
劉二 政治 73
-----------------------------
生成報表如下:
姓名 語文名次 數學名次 政治名次 總分名次
張三 3 2 1 2
李四 1 1 1
王五 1 3 3
劉二 4 3 4
*/
CREATE FUNCTION sort(
@oname varchar(10)
,@otype varchar(10)
)RETURNS int
AS
BEGIN
declare @id int,@score int,@score1 int
set @id=0
set @score=0
set @score1=0
select @score1=score from tb where otype=@otype and oname=@oname
select @id=case when score>=@score1 then @id+1 else @id+0 end from tb where otype=@otype
order by score desc
RETURN @id
END
go
drop table tb
create table tb
(
oname varchar(50)
,
otype varchar(50)
,
score int
)
insert into tb(oname,otype,score)
select '張三','語文',88
union all
select '張三','數學',100
union all
select '張三','政治',100
union all
select '李四','語文',90
union all
select '李四','數學',92
union all
select '王五','語文',70
union all
select '王五','數學',80
select oname,sum(case when otype='語文' then dbo.sort(oname,'語文') end ) as 語文名次,
sum(case when otype='數學' then dbo.sort(oname,'數學') end ) as 數學名次,
sum(case when otype='政治' then dbo.sort(oname,'政治') end ) as 政治名次
from tb
group by oname
order by oname
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -