PostgreSQL支持此查询,但是由于Over(partition by),H2无法运行查询。问题是如何只选择两行中具有不同值的最新创建时间的一行。
Example:
id name created ecid psid
1 aa 2019-02-07 1 1
2 bb 2019-02-01 1 1
3 cc 2019-02-05 2 2
4 dd 2019-02-06 2 3
5 ee 2019-02-08 2 3
Result:
id name created ecid psid
1 aa 2019-02-07 1 1
3 cc 2019-02-05 2 2
5 ee 2019-02-08 2 3
SELECT s.*, MAX(s.created) OVER (PARTITION BY s.ecid, s.psid) AS latest FROM ...
WHERE latest = created
答案 0 :(得分:1)
使用相关子查询
select t1.* from table t1
where t1.created = ( select max(created)
from table t2 where t1.ecid=t2.ecid and t1.psid=t2.psid)
答案 1 :(得分:0)
使用NOT EXISTS
:
select t.* from tablename t
where not exists (
select 1 from tablename
where ecid = t.ecid and psid = t.psid and created > t.created
)