这是我的表名tblPE
PEID |idnum | PE_DATE_EXAM | ATTENDANCE
1 | 39 | 2014-08-01 | PRESENT
2 | 42 | 2014-08-10 | ABSENT
3 | 39 | 2014-08-12 | PRESENT
4 | 43 | 2014-08-05 | PRESENT
5 | 42 | 2014-07-15 | NULL
6 | 39 | 2014-07-03 | ABSENT
7 | 41 | 2014-08-01 | PRESENT
我想选择最大的PE_DATE_EXAM值,其中idnum = 39且ATTENDANCE = PReSENT
结果应该是:
PEID |idnum | PE_DATE_EXAM | ATTENDANCE
-------------------------------------------
3 | 39 | 2014-08-12 | PRESENT
答案 0 :(得分:3)
对于单个记录返回,您可以使用LIMIT
:
SELECT * FROM tblPE t
WHERE t.idnum = 39 AND t.ATTENDANCE = 'PRESENT'
ORDER BY t.PE_DATE_EXAM DESC
LIMIT 1;
如果您希望所有idnum
动态,则可以使用NOT EXISTS()
:
SELECT * FROM tblPE t
WHERE NOT EXISTS(SELECT 1 FROM tblPE s
WHERE t.idnum = s.idnum AND s.ATTENDANCE = 'PRESENT')
AND t.attendance = 'PRESENT'
答案 1 :(得分:0)
select PEID,IDNUM,PE_DATE_EXAM,ATTENDANCE
from
(select PEID,IDNUM,PE_DATE_EXAM,ATTENDANCE,dense_rank() over(order by PE_DATE_EXAM desc) rnum
from TBLPE
order by PE_DATE_EXAM desc)
where rnum = '1';
此查询是根据您的查询结果编写的。如果您需要任何澄清,请告诉我。