34,876
社区成员
发帖
与我相关
我的任务
分享SELECT a.s#,sname,c001,c002,c003,avg_score
FROM
(
SELECT TOP 3 s#,
MAX(CASE WHEN c#='001' THEN score ELSE 0 END) c001,
MAX(CASE WHEN c#='001' THEN score ELSE 0 END) c002,
MAX(CASE WHEN c#='001' THEN score ELSE 0 END) c003,
MAX(CASE WHEN c#='001' THEN score ELSE 0 END) c004,
AVG(score) avg_score
FROM sc
WHERE c# IN ('001','002','003','004')
GROUP BY s#
HAVING
ss<
(SELECT MIN(x) mx
FROM
(
SELECT TOP 2 SUM(score) ss
FROM sc
WHERE c# IN ('001','002','003','004')
GROUP BY s#
ORDER BY ss
) a
)
ORDER BY ss DESC
) a
ORDER BY avg_scoreselect Student.S#,Sname,
max(case C# when 001 then score end) as 企业管理,
max(case C# when 002 then score end) as 马克思,
max(case C# when 003 then score end) as UML,
max(case C# when 004 then score end) as 数据库,
avg(score) as 平均成绩
from Student inner join SC on Student.S#=SC.S#
and Student.S# in (
select S#
from SC t1
where (select count(*) from SC where C#=t1.C# and score>=t1.score) between 3 and 6
and C# in (001,002,003,004)
)
group by Student.S#,Snameselect Student.S#,Sname,
max(case C# when 001 then score end) as 企业管理,
max(case C# when 002 then score end) as 马克思,
max(case C# when 003 then score end) as UML,
max(case C# when 004 then score end) as 数据库,
avg(score) as 平均成绩
from Student inner join SC on Student.S#=SC.S#
and S# in (
select S#
from SC t1
where (select count(*) from SC where C#=t1.C# and score>=t1.score) between 3 and 6
and C# in (001,002,003,004)
)
group by Student.S#,SnameSELECT DISTINCT top 3
SC.S# As 学生学号,
Student.Sname AS 学生姓名,
T1.score AS 企业管理,
T2.score AS 马克思,
T3.score AS UML,
T4.score AS 数据库,
ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) as 总分
FROM Student,SC LEFT JOIN SC AS T1
ON SC.S# = T1.S# AND T1.C# = '001'
LEFT JOIN SC AS T2
ON SC.S# = T2.S# AND T2.C# = '002'
LEFT JOIN SC AS T3
ON SC.S# = T3.S# AND T3.C# = '003'
LEFT JOIN SC AS T4
ON SC.S# = T4.S# AND T4.C# = '004'
WHERE student.S#=SC.S# AND
ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)
NOT IN
(SELECT
DISTINCT
TOP 15 WITH TIES
ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)
FROM sc
LEFT JOIN sc AS T1
ON sc.S# = T1.S# AND T1.C# = 'k1'
LEFT JOIN sc AS T2
ON sc.S# = T2.S# AND T2.C# = 'k2'
LEFT JOIN sc AS T3
ON sc.S# = T3.S# AND T3.C# = 'k3'
LEFT JOIN sc AS T4
ON sc.S# = T4.S# AND T4.C# = 'k4'
ORDER BY ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) DESC);
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/09/25/4592668.aspxdeclare @t1 table(id int ,name char(10))
declare @t2 table(id int ,result int)
insert into @t1
select 1,'wang' union
select 2,'hero' union
select 3,'wyq'
insert into @t2
select 1,40 union all
select 1,90 union all
select 1,50 union all
select 1,78 union all
select 2,89 union all
select 2,34 union all
select 2,67 union all
select 2,75
--获取每个id的最大的两个值(相当于内连接)
select *
from @t1 s cross apply (select top 2 * from @t2 t where id=s.id order by result desc) t
--获取每个id的最大的两个值(相当于外连接)
select *
from @t1 s outer apply (select top 2 * from @t2 t where id=s.id order by result desc) t
--获取每个id的第二个和第三个值
select *
from @t1 s cross apply (select * from (select row=row_number() over (partition by id order
by result desc),* from @t2)K where id=s.id and row between 2 and 4)K SELECT DISTINCT top 3
SC.S# As 学生学号,
Student.Sname AS 学生姓名 ,
T1.score AS 企业管理,
T2.score AS 马克思,
T3.score AS UML,
T4.score AS 数据库,
ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) as 总分
FROM Student,SC LEFT JOIN SC AS T1
ON SC.S# = T1.S# AND T1.C# = '001'
LEFT JOIN SC AS T2
ON SC.S# = T2.S# AND T2.C# = '002'
LEFT JOIN SC AS T3
ON SC.S# = T3.S# AND T3.C# = '003'
LEFT JOIN SC AS T4
ON SC.S# = T4.S# AND T4.C# = '004'
WHERE student.S#=SC.S# and
ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)
NOT IN
(SELECT
DISTINCT
TOP 15 WITH TIES
ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)
FROM sc
LEFT JOIN sc AS T1
ON sc.S# = T1.S# AND T1.C# = 'k1'
LEFT JOIN sc AS T2
ON sc.S# = T2.S# AND T2.C# = 'k2'
LEFT JOIN sc AS T3
ON sc.S# = T3.S# AND T3.C# = 'k3'
LEFT JOIN sc AS T4
ON sc.S# = T4.S# AND T4.C# = 'k4'
ORDER BY ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) DESC);