Skip to content
Advertisement

Select only one row by more conditions

This query is supported by PostgreSQL but H2 can not run query because of Over(partition by) . Question is how to select only one row with latest created time for different values in 2 columns.

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

Advertisement

Answer

use correlated subquery

select t1.* from table t1
where t1.created = ( select max(created) 
from table t2 where t1.ecid=t2.ecid and t1.psid=t2.psid)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement