CREATE TABLE grades ( tname char(10) not null, fr_grade int not null, sql_grade int not null, tomcat_grade int not null, kettle_grade int not null, inserttime varchar2(10) not null, primary key (tname, inserttime) ); 以上列语句分析下列查询需求 若通过一次考核则视为人员考核通过,若考核通过则输出考核通过中总分最高的记录,若考核未通过则输出考核未通过中总分最高的记录, 结果输出:人员姓名、考核结果、FR得分、SQL得分、TOMCAT得分、kettle得分、考核时间
时间: 2024-03-24 17:39:04 浏览: 176
配置Event_alert
可以使用以下SQL语句实现需求:
```
SELECT tname,
CASE
WHEN fr_grade >= 60 AND sql_grade >= 60 AND tomcat_grade >= 60 AND kettle_grade >= 60 THEN '通过'
ELSE '未通过'
END AS 考核结果,
fr_grade,
sql_grade,
tomcat_grade,
kettle_grade,
inserttime
FROM grades
WHERE tname IN (SELECT tname FROM grades WHERE fr_grade >= 60 AND sql_grade >= 60 AND tomcat_grade >= 60 AND kettle_grade >= 60
UNION
SELECT tname FROM grades WHERE fr_grade < 60 OR sql_grade < 60 OR tomcat_grade < 60 OR kettle_grade < 60)
AND inserttime IN (SELECT inserttime FROM grades WHERE fr_grade >= 60 AND sql_grade >= 60 AND tomcat_grade >= 60 AND kettle_grade >= 60
UNION
SELECT inserttime FROM grades WHERE fr_grade < 60 OR sql_grade < 60 OR tomcat_grade < 60 OR kettle_grade < 60)
AND (fr_grade + sql_grade + tomcat_grade + kettle_grade) = (SELECT MAX(fr_grade + sql_grade + tomcat_grade + kettle_grade) FROM grades WHERE tname IN (SELECT tname FROM grades WHERE fr_grade >= 60 AND sql_grade >= 60 AND tomcat_grade >= 60 AND kettle_grade >= 60
UNION
SELECT tname FROM grades WHERE fr_grade < 60 OR sql_grade < 60 OR tomcat_grade < 60 OR kettle_grade < 60)
AND inserttime IN (SELECT inserttime FROM grades WHERE fr_grade >= 60 AND sql_grade >= 60 AND tomcat_grade >= 60 AND kettle_grade >= 60
UNION
SELECT inserttime FROM grades WHERE fr_grade < 60 OR sql_grade < 60 OR tomcat_grade < 60 OR kettle_grade < 60)))
```
该语句使用了多层嵌套的子查询,首先在最外层查询中筛选出符合要求的记录,其中 `tname` 与 `inserttime` 都需要在通过考核和未通过考核的记录中出现过。然后,通过子查询计算出通过考核或未通过考核中总分最高的记录,最后通过主查询输出结果。注意,如果存在多个总分相同的最高记录,则会全部输出。
阅读全文