27,579
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE [dbo].[stu](
[姓名] [nvarchar](4) NULL,
[语文] [int] NULL,
[数学] [int] NULL,
[英语] [int] NULL,
[语文名次] [int] NULL,
[数学名次] [int] NULL,
[英语名次] [int] NULL
) ON [PRIMARY]
update stu set 语文名次= (select count(*) from stu b where a.语文<b.语文) +1,
数学名次 = (select count(*) from stu c where a.数学<c.数学)+1,
英语名次= (select count(*) from stu d where a.英语<d.英语)+1
from stu a
select * from stu
select t.姓名,t.语文,t.数学,t.英语,
语文排名 = (select count(1) from stu where 语文 > t.语文) + 1,
数学排名 = (select count(1) from stu where 数学 > t.数学) + 1,
英语排名 = (select count(1) from stu where 英语 > t.英语) + 1,
总排名 = (select count(1) from stu where 语文+数学+英语 > t.语文+t.数学+t,英语) + 1
from stu t
select t.姓名,t.语文,t.数学,t.英语,
语文排名 = (select count(1) from stu where 语文 < t.语文) + 1,
数学排名 = (select count(1) from stu where 数学 < t.数学) + 1,
英语排名 = (select count(1) from stu where 英语 < t.英语) + 1
from stu t
declare @a table (姓名 varchar(10),语文 int,数学 int,英语 int)
insert into @a values ('张三',99,100,85)
insert into @a values ('张三',95,80,100)
insert into @a values ('张三',96,45,37)
select * from @a
select 姓名
,语文
,数学
,英语
,rank() over(order by 语文 desc) as 语文排名
,rank() over(order by 数学 desc) as 数学排号
,rank() over(order by 英语 desc) as 英语排名
from @a
declare @t table(姓名 varchar(10),语文 int,数学 int,英语 int,语文排名 int,数学排名 int,英语排名 int)
insert into @t select '张三',99,100,85,0,0,0
insert into @t select '李四',95,80,100,0,0,0
insert into @t select '王五',96,45,37,0,0,0
update @t
set 语文排名=(select count(1) from @t where 语文>a.语文)+1,
数学排名=(select count(1) from @t where 数学>a.数学)+1,
英语排名=(select count(1) from @t where 英语>a.英语)+1
from @t a
select * from @t
create table stu (姓名 varchar(10),语文 int,数学 int,英语 int,语文排名 int,数学排号 int,英语排名 int)
insert into stu(姓名,语文,数学,英语)
select '张三',99,100,85
union all select '李四',95,80,100
union all select '王五',96,45,37
update stu
set 语文排名=(select count(*) from stu b where a.语文<b.语文)+1,
数学排号=(select count(*) from stu c where a.数学<c.数学)+1,
英语排名=(select count(*) from stu d where a.英语<d.英语)+1
from stu a
select * from stu
/*
姓名 语文 数学 英语 语文排名 数学排号 英语排名
---------- ----------- ----------- ----------- ----------- ----------- -----------
张三 99 100 85 1 1 2
李四 95 80 100 3 2 1
王五 96 45 37 2 3 3
(所影响的行数为 3 行)
*/
create table stu(name varchar(10), chinese int,math int,english int, c_id int,m_id int,e_id int)
insert into stu(name,chinese,math,english) select 'A',99,100,80
insert into stu(name,chinese,math,english) select 'B',95,80,100
insert into stu(name,chinese,math,english) select 'C',96,45,37
update stu
set c_id=1+isnull((select count(*) from stu A where A.chinese>stu.chinese),0),
m_id=1+isnull((select count(*) from stu A where A.math>stu.math),0),
e_id=1+isnull((select count(*) from stu A where A.english>stu.english),0)
select * from stu
/*
A 99 100 80 1 1 2
B 95 80 100 3 2 1
C 96 45 37 2 3 3
*/
drop table stu