http://community.csdn.net/Expert/topic/5139/5139700.xml?temp=.4085504
谢谢各位在这个帖子里面的回答,但是经过我的测试,发现有问题测试数据
student_id teacher_id course_id percentMark
010 001 001 91.0
009 001 001 92.0
008 001 001 93.0
007 001 001 94.0
006 001 001 95.0
005 001 001 96.0
004 001 001 97.0
003 001 001 98.0
002 001 001 99.0
001 001 001 100.0
001 002 001 91.0
002 002 001 92.0
003 002 001 93.0
004 002 001 94.0
005 002 001 95.0
006 002 001 96.0
007 002 001 97.0
008 002 001 98.0
009 002 001 99.0
010 002 001 100.0希望的到的结果
010 001 001 91.0 (本行没有)
009 001 001 92.0
008 001 001 93.0
007 001 001 94.0
006 001 001 95.0
005 001 001 96.0
004 001 001 97.0
003 001 001 98.0
002 001 001 99.0
001 001 001 100.0 (本行没有)
001 002 001 91.0 (本行没有)
002 002 001 92.0
003 002 001 93.0
004 002 001 94.0
005 002 001 95.0
006 002 001 96.0
007 002 001 97.0
008 002 001 98.0
009 002 001 99.0
010 002 001 100.0 (本行没有)
看看该如何处理?就是去掉每个老师得分中的10%最高分和10%最低分,呵呵
谢谢各位在这个帖子里面的回答,但是经过我的测试,发现有问题测试数据
student_id teacher_id course_id percentMark
010 001 001 91.0
009 001 001 92.0
008 001 001 93.0
007 001 001 94.0
006 001 001 95.0
005 001 001 96.0
004 001 001 97.0
003 001 001 98.0
002 001 001 99.0
001 001 001 100.0
001 002 001 91.0
002 002 001 92.0
003 002 001 93.0
004 002 001 94.0
005 002 001 95.0
006 002 001 96.0
007 002 001 97.0
008 002 001 98.0
009 002 001 99.0
010 002 001 100.0希望的到的结果
010 001 001 91.0 (本行没有)
009 001 001 92.0
008 001 001 93.0
007 001 001 94.0
006 001 001 95.0
005 001 001 96.0
004 001 001 97.0
003 001 001 98.0
002 001 001 99.0
001 001 001 100.0 (本行没有)
001 002 001 91.0 (本行没有)
002 002 001 92.0
003 002 001 93.0
004 002 001 94.0
005 002 001 95.0
006 002 001 96.0
007 002 001 97.0
008 002 001 98.0
009 002 001 99.0
010 002 001 100.0 (本行没有)
看看该如何处理?就是去掉每个老师得分中的10%最高分和10%最低分,呵呵
解决方案 »
- 取最大值的问题,如何修改sql
- 请教一个关于Update sql语句
- 菜鸟问题
- select ccode,ccname form t1 where 1=2 中的 where 1=2 如何理解
- 任何一个二目关系都属于三范式吗???
- 两个表中的数据相对应
- select 'it's up to you!' 中的单引号问题
- 实现把动态文件名的Excel中的数据传入到SQL时出现'Microsoft.Jet.OLEDB.4.0' 附近有语法错误
- 如何根据表的一个字段查找另一表的字段并返回
- 替代group by的方法
- 存储过程中使用字符串动态生成sql语句,会影响效率或性能么?
- 如何显示nvarchar字段?
insert into @t select '010','001','001',91.0
insert into @t select '009','001','001',92.0
insert into @t select '008','001','001',93.0
insert into @t select '007','001','001',94.0
insert into @t select '006','001','001',95.0
insert into @t select '005','001','001',96.0
insert into @t select '004','001','001',97.0
insert into @t select '003','001','001',98.0
insert into @t select '002','001','001',99.0
insert into @t select '001','001','001',100.0
insert into @t select '001','002','001',91.0
insert into @t select '002','002','001',92.0
insert into @t select '003','002','001',93.0
insert into @t select '004','002','001',94.0
insert into @t select '005','002','001',95.0
insert into @t select '006','002','001',96.0
insert into @t select '007','002','001',97.0
insert into @t select '008','002','001',98.0
insert into @t select '009','002','001',99.0
insert into @t select '010','002','001',100.0
select
t.*
from
@t t
where
t.student_id not in(select top 10 percent student_id from @t where teacher_id=t.teacher_id and course_id=t.course_id order by percentMark desc)
and
t.student_id not in(select top 10 percent student_id from @t where teacher_id=t.teacher_id and course_id=t.course_id order by percentMark asc)/*
student_id teacher_id course_id percentMark
---------- ---------- --------- -----------
009 001 001 92.0
008 001 001 93.0
007 001 001 94.0
006 001 001 95.0
005 001 001 96.0
004 001 001 97.0
003 001 001 98.0
002 001 001 99.0
002 002 001 92.0
003 002 001 93.0
004 002 001 94.0
005 002 001 95.0
006 002 001 96.0
007 002 001 97.0
008 002 001 98.0
009 002 001 99.0
*/
insert into @t select '010','001','001',91.0
insert into @t select '009','001','001',92.0
insert into @t select '008','001','001',93.0
insert into @t select '007','001','001',94.0
insert into @t select '006','001','001',95.0
insert into @t select '005','001','001',96.0
insert into @t select '004','001','001',97.0
insert into @t select '003','001','001',98.0
insert into @t select '002','001','001',99.0
insert into @t select '001','001','001',100.0
insert into @t select '001','002','001',91.0
insert into @t select '002','002','001',92.0
insert into @t select '003','002','001',93.0
insert into @t select '004','002','001',94.0
insert into @t select '005','002','001',95.0
insert into @t select '006','002','001',96.0
insert into @t select '007','002','001',97.0
insert into @t select '008','002','001',98.0
insert into @t select '009','002','001',99.0
insert into @t select '010','002','001',100.0-- 删除
DELETE A
FROM @t A, (
SELECT gid = NTILE(10) OVER(PARTITION BY teacher_id ORDER BY percentMark), id
FROM @t
)B
WHERE A.id = b.id
AND B.gid IN(1, 10)-- 结果
SELECT * FROM @t
即类似这样处理:declare @t table(id int identity(1,1), student_id varchar(8),teacher_id varchar(8),course_id varchar(8),percentMark numeric(4,1))
insert into @t select '010','001','001',91.0
insert into @t select '009','001','001',92.0
insert into @t select '008','001','001',93.0
insert into @t select '007','001','001',94.0
insert into @t select '006','001','001',95.0
insert into @t select '005','001','001',96.0
insert into @t select '004','001','001',97.0
insert into @t select '003','001','001',98.0
insert into @t select '002','001','001',99.0
insert into @t select '001','001','001',100.0
insert into @t select '001','002','001',91.0
insert into @t select '002','002','001',92.0
insert into @t select '003','002','001',93.0
insert into @t select '004','002','001',94.0
insert into @t select '005','002','001',95.0
insert into @t select '006','002','001',96.0
insert into @t select '007','002','001',97.0
insert into @t select '008','002','001',98.0
insert into @t select '009','002','001',99.0
insert into @t select '010','002','001',100.0-- 生成临时表
SELECT id = id * 1, gid = IDENTITY(int, 1, 1), teacher_id
INTO #
FROM @t
ORDER BY teacher_id, percentMark-- 从临时表中选出最前和最后10%的记录, 然后与原表关联做删除
DELETE A
FROM @t A, (
SELECT A.id, gid = (A.gid - B.gid) / B.g
FROM # A,(
SELECT
teacher_id,
gid = MIN(gid),
g = CONVERT(int, CEILING(COUNT(*) / 10.))
FROM #
GROUP BY teacher_id
)B
WHERE A.teacher_id = B.teacher_id
)B
WHERE A.id = B.id
AND B.gid IN(0, 9)-- 结果
SELECT * FROM @t
GODROP TABLE #