有这样两张表,
人员成绩表:id,人员ID,科目id,科目成绩
称号评审表:id,称号id,科目ID,科目成绩,科目分组
分析:一个人可以有多种科目记录,每种科目记录都有一个科目成绩。
一个称号要求有多个科目,以及科目的及格成绩。
一个人如果要获取某个称号,必须具有称号要求的所有科目,且成绩要大于等于称号要求的成绩。
当称号中的某两个科目为一组时,人员只需满足这一组中的一个以及没有组的所有即可。不要存储过程和函数,要用一个sql语句解决,真正的大牛感兴趣了可以试试!
如果有可用的关键字也可以说说!
参与就有分送!!!sql数据比对
人员成绩表:id,人员ID,科目id,科目成绩
称号评审表:id,称号id,科目ID,科目成绩,科目分组
分析:一个人可以有多种科目记录,每种科目记录都有一个科目成绩。
一个称号要求有多个科目,以及科目的及格成绩。
一个人如果要获取某个称号,必须具有称号要求的所有科目,且成绩要大于等于称号要求的成绩。
当称号中的某两个科目为一组时,人员只需满足这一组中的一个以及没有组的所有即可。不要存储过程和函数,要用一个sql语句解决,真正的大牛感兴趣了可以试试!
如果有可用的关键字也可以说说!
参与就有分送!!!sql数据比对
create table t_person_course(id number(11),person_id varchar2(16),course_id varchar2(16),course_score number(3));
insert into t_person_course values(1,'123','1213',6);
insert into t_person_course values(2,'123','1215',7);
insert into t_person_course values(3,'123','1216',4);
insert into t_person_course values(4,'124','1213',4);
insert into t_person_course values(5,'124','1217',6);
insert into t_person_course values(6,'125','1216',3);
insert into t_person_course values(7,'125','1215',6);create table t_title_course(id number(11),title_id varchar2(16),course_id varchar2(16),course_score number(3),dis_group varchar2(8));
insert into t_title_course values(1,'10','1216',4,null);
insert into t_title_course values(1,'10','1215',6,'z1');
insert into t_title_course values(1,'10','1217',3,'z1');---写一句sql判断id为‘123’、‘124’、‘125’的三位学员是否能获得id为‘10’的称号。
---分析结果,只有学员‘123’符合要求,学员‘124’缺少科目‘1216’,学员‘125’科目‘1216’的得分不够
from
(
select a.person_id,a.course_score,b.title_id,b.dis_group
from t_person_course a,t_title_course b
where a.course_id = b.course_id and a.course_score >= b.course_score and b.dis_group is not null
) t
where 1=1
group by person_id,title_id
having count(*) = (select count(*) from t_title_course where dis_group is not null)
也就是说,一个称号要求的科目,没有组的必须都要满足,同时要满足每一组中的最少一个才能获得称号。
而现在的要求是,给出person_id 和title_id,判断这个人是否能获得这个称号
from (select distinct a.title_id,
a.course_score,
a.course_id,
b.person_id,
b.course_score
from (select distinct a.TITLE_ID,
a.COURSE_SCORE,
a.course_id,
b.person_id
from t_title_course a, t_person_course b
where a.DIS_GROUP is null) a
left join t_person_course b
on a.course_id = b.course_id
and a.PERSON_ID = b.PERSON_ID
where b.COURSE_SCORE >= a.COURSE_SCORE) c, --检查符合没有组的有哪些PERSON_ID
(select distinct a.title_id,
a.course_score,
a.course_id,
b.person_id,
b.course_score
from (select distinct a.TITLE_ID,
a.COURSE_SCORE,
a.course_id,
b.person_id
from t_title_course a, t_person_course b
where a.DIS_GROUP is not null) a
left join t_person_course b
on a.course_id = b.course_id
and a.PERSON_ID = b.PERSON_ID
where b.COURSE_SCORE >= a.COURSE_SCORE) d --检查符合有组的有哪些PERSON_ID
where c.PERSON_ID = d.PERSON_ID
and c.title_id = d.title_id
select t.person_id,t.title_id
from
(
select a.person_id,a.course_score,b.title_id,b.dis_group
from t_person_course a,t_title_course b
where a.course_id = b.course_id and a.course_score >= b.course_score and b.dis_group is not null
) t,t_person_course t1,t_title_course t2
where t.person_id = t1.person_id and t1.course_id = t2.course_id
and t1.course_score >= t2.course_score and t2.dis_group is null
group by t.person_id,t.title_id