sql - SQL: How to select pass rows from student table in case of re-sit exam in postgresql?
问题描述
Say I have a table which has the data of students and their results.
ID Result
1 PASS
2 PASS
3 PASS
3 FAIL
4 PASS
4 FAIL
4 FAIL
5 FAIL
5 FAIL
5 FAIL
And I want to select one row for each student if they pass, I want pass rows to return but if they don't then return one fail row.
This is the expected result:
ID Result
1 PASS
2 PASS
3 PASS
4 PASS
5 FAIL
Note: Students can re-sit the exam as many times as they wish until they get a pass or give up from the exam.
Thank you.
解决方案
Do a GROUP BY
. Since PASS
(always) is greater than FAIL
, use MAX()
.
select ID, max(Result)
from tablename
group by ID
推荐阅读
- javascript - React Native 无法在异步函数中正确设置超时
- vue.js - 使用插槽范围问题的元素 UI 表列“无法读取未定义的属性 'column_name'”
- wso2 - WSO2:覆盖 MQ 的 JMS 生产者 JNDI 属性
- gitlab - gitlab runner 并发如何工作?
- sql - 蜂巢中的简单表格转置
- html - Angular 6动画不显示
- javascript - 与 JavaScript 和 PHP 相关的语法
- algorithm - 以递归方式编辑距离相似算法
- c# - C#通过引用传递数组
- ruby-on-rails - Rails 5.2 credentials.yml.enc ERB 不解析