Sql 按多个条件仅选择一行
PostgreSQL支持此查询,但H2无法运行查询,因为用户过度分配。问题是如何为两列中的不同值仅选择具有最新创建时间的一行Sql 按多个条件仅选择一行,sql,h2,greatest-n-per-group,Sql,H2,Greatest N Per Group,PostgreSQL支持此查询,但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 201
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
使用相关子查询
select t1.* from table t1
where t1.created = ( select max(created)
from table t2 where t1.ecid=t2.ecid and t1.psid=t2.psid)
不存在以下用途:
所以你们实际上是在寻找一个对H2有效的解决方案,而不是对Postgres?是的,我在寻找一个对H2有效的解决方案
select t.* from tablename t
where not exists (
select 1 from tablename
where ecid = t.ecid and psid = t.psid and created > t.created
)