select * from tablename where 得分 not in(select min(得分) from tablename group by 歌手 union all select max(得分) from tablename group by 歌手)
select * from tablename as a where 得分 not in(select min(得分) from tablename where 歌手=a. 歌手 union all select max(得分) from tablename where 歌手=a. 歌手)
select * from tablename where 得分 not in(select min(得分) from tablename group by 歌手 union all select max(得分) 歌手 from tablename)
我是个初学者,我认为应该是这样写的不知道对不对的!select 歌手 from tablename where 得分 not in(select min(得分) from tablename union all select max(得分) from tablename) grop by 歌手
select a.* from tb a where not exists (select 1 from (select 歌手,min(得分) as 最低分,max(得分) as 最高分 from tb group by 歌手) b where a.歌手=b.歌手 and a.得分 in (b.最低分 ,最高分) )
select 歌手, sum( 得分)-(select min(得分) from t1 where 歌手=a.歌手 )-(select max(得分) from t1 where 歌手=a.歌手 ) from t1 a group by 歌手
create table tb (评委 varchar(1) , 歌手 int, 得分 int)insert into tb select 'A' , 1 , 75 union all select 'A' , 2 , 50 union all select 'A' , 3 , 78 union all select 'A' , 4 , 66 union all select 'B' , 1 , 88 union all select 'B' , 2 , 80 union all select 'B' , 3 , 77 union all select 'B' , 4 , 67 union all select 'C' , 1 , 70 union all select 'C' , 2 , 60 union all select 'C' , 3 , 90 union all select 'C' , 4 , 79 union all select 'D' , 1 , 85 union all select 'D' , 2 , 68 union all select 'D' , 3 , 89 union all select 'D' , 4 , 70select a.* from tb a where not exists (select 1 from (select 歌手,min(得分) as 最低分,max(得分) as 最高分 from tb group by 歌手) b where a.歌手=b.歌手 and a.得分 in (b.最低分 ,b.最高分) ) order by a.歌手,a.得分 A 1 75 D 1 85 C 2 60 D 2 68 A 3 78 D 3 89 B 4 67 D 4 70
加个别名select 歌手, sum( 得分)-(select min(得分) from t1 where 歌手=a.歌手 )-(select max(得分) from t1 where 歌手=a.歌手) as 最后得分 from t1 a group by 歌手歌手 最后得分 ----------- ----------- 1 160 2 128 3 167 4 137(4 row(s) affected)
select 歌手,最后得分=sum(得分)-(select max(得分)+min(得分) from tb where a.歌手=歌手) from tb a group by 歌手 歌手 最后得分 ----------- ----------- 1 160 2 128 3 167 4 137(4 row(s) affected)
select 歌手,sum( 得分)-min(得分)-max(得分) as 最后得分 from tb group by 歌手 就可以了 1 160 2 128 3 167 4 137 如果求平均数,则用 select 歌手,(sum( 得分)-min(得分)-max(得分))/(COUNT(*)-2) as 最后得分 from tb group by 歌手1 80 2 64 3 83 4 68
如果隻是計算歌手的總分的話,可以這樣查詢 SELECT 歌手, SUM(得分) - MAX(得分) - MIN(得分) AS 總得分 FROM 表名 GROUP BY 歌手 ORDER BY 得分
用max 和 min 肯定不对,最高分有重复原的.
大家从select top* 1 的角度想想,谢谢各位了
select 歌手,评委,avgfen=avg(得分) from deltop t where not exists(select 1 from (select top 1 * from deltop order by 歌手,评委,得分) a where t.歌手=a.歌手 and t.评委=a.评委 ) and not exists(select 1 from (select top 1 * from deltop order by 歌手,评委, 得分 desc) a where t.歌手=a.歌手 and t.评委=a.评委) group by 歌手,评委 order by 歌手,评委
你是去掉一个最高分,又不是去掉所有的最高分,用MAX当然可以,你用例子试试就知 我试过了,可以的
select * from tb b where not exists ( select 1 from (select min(评委) as 评委,歌手,得分 from ( select * from tb a where not exists (select 1 from tb where 歌手=a.歌手 and 得分>a.得分) ) c group by 歌手,得分
union allselect min(评委) as 评委,歌手,得分 from ( select * from tb a where not exists (select 1 from tb where 歌手=a.歌手 and 得分<a.得分) ) c group by 歌手,得分 ) a where a.评委=b.评委 and a.歌手=b.歌手 and a.得分=b.得分 ) order by b.歌手,b.得分
--用这个数据测试, A 1 75 C 1 75 D 1 88 B 1 88 A 2 50 C 2 60 D 2 68 B 2 80 B 3 77 A 3 78 D 3 89 C 3 90 A 4 66 B 4 67 D 4 70 C 4 79select * from tb b where not exists ( select 1 from (select min(评委) as 评委,歌手,得分 from ( select * from tb a where not exists (select 1 from tb where 歌手=a.歌手 and 得分>a.得分) ) c group by 歌手,得分
union allselect min(评委) as 评委,歌手,得分 from ( select * from tb a where not exists (select 1 from tb where 歌手=a.歌手 and 得分<a.得分) ) c group by 歌手,得分 ) a where a.评委=b.评委 and a.歌手=b.歌手 and a.得分=b.得分 ) order by b.歌手,b.得分------------------------------C 1 75 D 1 88 C 2 60 D 2 68 A 3 78 D 3 89 B 4 67 D 4 70
楼上数据不具有代表性 我提供组数据先 1有2个最高分88,2有2个最低分50 Declare @tmpTable Table(id int identity(1,1),appraiser varchar(50),player varchar(50),score int)insert into @tmpTable values('A','1',75) insert into @tmpTable values('A','2',50) insert into @tmpTable values('A','3',78) insert into @tmpTable values('A','4',66) insert into @tmpTable values('B','1',88) insert into @tmpTable values('B','2',80) insert into @tmpTable values('B','3',77) insert into @tmpTable values('B','4',67) insert into @tmpTable values('C','1',70) insert into @tmpTable values('C','2',50) insert into @tmpTable values('C','3',90) insert into @tmpTable values('C','4',79) insert into @tmpTable values('D','1',88) insert into @tmpTable values('D','2',68) insert into @tmpTable values('D','3',89) insert into @tmpTable values('D','4',70)
--按照楼上的意思 A 1 75 C 1 75 D 1 88 B 1 88 A 2 50 C 2 50 D 2 68 B 2 80 B 3 77 A 3 78 D 3 89 C 3 90 A 4 66 B 4 66 D 4 70 C 4 79--依旧原来的代码 select * from tb b where not exists ( select 1 from (select min(评委) as 评委,歌手,得分 from ( select * from tb a where not exists (select 1 from tb where 歌手=a.歌手 and 得分>a.得分) ) c group by 歌手,得分
union allselect min(评委) as 评委,歌手,得分 from ( select * from tb a where not exists (select 1 from tb where 歌手=a.歌手 and 得分<a.得分) ) c group by 歌手,得分 ) a where a.评委=b.评委 and a.歌手=b.歌手 and a.得分=b.得分 ) order by b.歌手,b.得分 ------------------------------ 返回 -----------------------------C 1 75 D 1 88 C 2 50 D 2 68 A 3 78 D 3 89 B 4 66 D 4 70
where 得分 not in(select min(得分) from tablename group by 歌手
union all
select max(得分) from tablename group by 歌手)
where 得分 not in(select min(得分) from tablename where 歌手=a. 歌手
union all
select max(得分) from tablename where 歌手=a. 歌手)
where 得分 not in(select min(得分) from tablename group by 歌手 union all
select max(得分) 歌手 from tablename)
where 得分 not in(select min(得分) from tablename union all
select max(得分) from tablename)
grop by 歌手
(select 1 from
(select 歌手,min(得分) as 最低分,max(得分) as 最高分 from tb group by 歌手) b
where a.歌手=b.歌手 and a.得分 in (b.最低分 ,最高分) )
sum( 得分)-(select min(得分) from t1 where 歌手=a.歌手 )-(select max(得分) from t1 where 歌手=a.歌手 )
from t1 a
group by 歌手
歌手
----------- -----------
1 160
2 128
3 167
4 137(4 row(s) affected)
(评委 varchar(1) ,
歌手 int,
得分 int)insert into tb
select 'A' , 1 , 75 union all
select 'A' , 2 , 50 union all
select 'A' , 3 , 78 union all
select 'A' , 4 , 66 union all
select 'B' , 1 , 88 union all
select 'B' , 2 , 80 union all
select 'B' , 3 , 77 union all
select 'B' , 4 , 67 union all
select 'C' , 1 , 70 union all
select 'C' , 2 , 60 union all
select 'C' , 3 , 90 union all
select 'C' , 4 , 79 union all
select 'D' , 1 , 85 union all
select 'D' , 2 , 68 union all
select 'D' , 3 , 89 union all
select 'D' , 4 , 70select a.* from tb a where not exists
(select 1 from
(select 歌手,min(得分) as 最低分,max(得分) as 最高分 from tb group by 歌手) b
where a.歌手=b.歌手 and a.得分 in (b.最低分 ,b.最高分) ) order by a.歌手,a.得分
A 1 75
D 1 85
C 2 60
D 2 68
A 3 78
D 3 89
B 4 67
D 4 70
sum( 得分)-(select min(得分) from t1 where 歌手=a.歌手 )-(select max(得分) from t1 where 歌手=a.歌手) as 最后得分
from t1 a
group by 歌手歌手 最后得分
----------- -----------
1 160
2 128
3 167
4 137(4 row(s) affected)
group by 歌手
歌手 最后得分
----------- -----------
1 160
2 128
3 167
4 137(4 row(s) affected)
from tb group by 歌手
就可以了
1 160
2 128
3 167
4 137
如果求平均数,则用
select 歌手,(sum( 得分)-min(得分)-max(得分))/(COUNT(*)-2) as 最后得分
from tb group by 歌手1 80
2 64
3 83
4 68
SELECT 歌手, SUM(得分) - MAX(得分) - MIN(得分) AS 總得分
FROM 表名
GROUP BY 歌手
ORDER BY 得分
where not exists(select 1 from (select top 1 * from deltop order by 歌手,评委,得分) a where t.歌手=a.歌手 and t.评委=a.评委 )
and not exists(select 1 from (select top 1 * from deltop order by 歌手,评委, 得分 desc) a where t.歌手=a.歌手 and t.评委=a.评委) group by 歌手,评委 order by 歌手,评委
我试过了,可以的
(
select 1 from (select min(评委) as 评委,歌手,得分 from (
select * from tb a
where not exists
(select 1 from tb where 歌手=a.歌手 and 得分>a.得分) ) c
group by 歌手,得分
union allselect min(评委) as 评委,歌手,得分 from (
select * from tb a
where not exists
(select 1 from tb where 歌手=a.歌手 and 得分<a.得分) ) c
group by 歌手,得分
) a where a.评委=b.评委 and a.歌手=b.歌手 and a.得分=b.得分
)
order by b.歌手,b.得分
A 1 75
C 1 75
D 1 88
B 1 88
A 2 50
C 2 60
D 2 68
B 2 80
B 3 77
A 3 78
D 3 89
C 3 90
A 4 66
B 4 67
D 4 70
C 4 79select * from tb b where not exists
(
select 1 from (select min(评委) as 评委,歌手,得分 from (
select * from tb a
where not exists
(select 1 from tb where 歌手=a.歌手 and 得分>a.得分) ) c
group by 歌手,得分
union allselect min(评委) as 评委,歌手,得分 from (
select * from tb a
where not exists
(select 1 from tb where 歌手=a.歌手 and 得分<a.得分) ) c
group by 歌手,得分
) a where a.评委=b.评委 and a.歌手=b.歌手 and a.得分=b.得分
)
order by b.歌手,b.得分------------------------------C 1 75
D 1 88
C 2 60
D 2 68
A 3 78
D 3 89
B 4 67
D 4 70
1有2个最高分88,2有2个最低分50
Declare @tmpTable Table(id int identity(1,1),appraiser varchar(50),player varchar(50),score int)insert into @tmpTable values('A','1',75)
insert into @tmpTable values('A','2',50)
insert into @tmpTable values('A','3',78)
insert into @tmpTable values('A','4',66)
insert into @tmpTable values('B','1',88)
insert into @tmpTable values('B','2',80)
insert into @tmpTable values('B','3',77)
insert into @tmpTable values('B','4',67)
insert into @tmpTable values('C','1',70)
insert into @tmpTable values('C','2',50)
insert into @tmpTable values('C','3',90)
insert into @tmpTable values('C','4',79)
insert into @tmpTable values('D','1',88)
insert into @tmpTable values('D','2',68)
insert into @tmpTable values('D','3',89)
insert into @tmpTable values('D','4',70)
A 1 75
C 1 75
D 1 88
B 1 88
A 2 50
C 2 50
D 2 68
B 2 80
B 3 77
A 3 78
D 3 89
C 3 90
A 4 66
B 4 66
D 4 70
C 4 79--依旧原来的代码
select * from tb b where not exists
(
select 1 from (select min(评委) as 评委,歌手,得分 from (
select * from tb a
where not exists
(select 1 from tb where 歌手=a.歌手 and 得分>a.得分) ) c
group by 歌手,得分
union allselect min(评委) as 评委,歌手,得分 from (
select * from tb a
where not exists
(select 1 from tb where 歌手=a.歌手 and 得分<a.得分) ) c
group by 歌手,得分
) a where a.评委=b.评委 and a.歌手=b.歌手 and a.得分=b.得分
)
order by b.歌手,b.得分
------------------------------
返回
-----------------------------C 1 75
D 1 88
C 2 50
D 2 68
A 3 78
D 3 89
B 4 66
D 4 70