WITH cte AS (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY PK1, PK2 ORDER BY timestamp DESC) rn
FROM yourTable t
)
SELECT PK1, PK2, value1, value2
FROM cte
WHERE rn = 1;
SELECT t1.* FROM youtable t1
JOIN
(SELECT MIN(timestamp) as minTimestamp,User FROM yourtable GROUP BY User) t2
ON t1.User=t2.User AND t1.timestamp = t2.minTimestamp
ORDER BY t1.User
3条答案
按热度按时间2nbm6dog1#
我们可以在这里使用
ROW_NUMBER()
:qc6wkl3g2#
除了蒂姆斯的回答,似乎
GROUP BY
也能做到这一点eqoofvh93#
另一种选择是使用PostgreSQL的专有
distinct on ()