सभी स्तंभों का चयन करने में सक्षम होने के लिए और न केवल object_id और MAX(event_timestamp) , आप DISTINCT ON . का उपयोग कर सकते हैं
SELECT DISTINCT ON (object_id)
object_id, event_timestamp ---, more columns
FROM test_select
ORDER BY object_id, event_timestamp DESC ;
यदि आप चाहते हैं कि परिणाम event_timestamp DESC by द्वारा क्रमित हों और object_id . द्वारा नहीं , आपको इसे किसी व्युत्पन्न तालिका या CTE में शामिल करना होगा:
SELECT *
FROM
( SELECT DISTINCT ON (object_id)
object_id, event_timestamp ---, more columns
FROM test_select
ORDER BY object_id, event_timestamp DESC
) AS t
ORDER BY event_timestamp DESC ;
वैकल्पिक रूप से, आप विंडो फ़ंक्शन का उपयोग कर सकते हैं, जैसे ROW_NUMBER() :
WITH cte AS
( SELECT ROW_NUMBER() OVER (PARTITION BY object_id
ORDER BY event_timestamp DESC)
AS rn,
object_id, event_timestamp ---, more columns
FROM test_select
)
SELECT object_id, event_timestamp ---, more columns
FROM cte
WHERE rn = 1
ORDER BY event_timestamp DESC ;
या कुल MAX() OVER . के साथ :
WITH cte AS
( SELECT MAX(event_timestamp) OVER (PARTITION BY object_id)
AS max_event_timestamp,
object_id, event_timestamp ---, more columns
FROM test_select
)
SELECT object_id, event_timestamp ---, more columns
FROM cte
WHERE event_timestamp = max_event_timestamp
ORDER BY event_timestamp DESC ;