2

I have a postgresql table wherein I have few fields such as id and date. I need to find the max date for that id and show the same into a new field for all the ids. SQLFiddle site was not responding so I have an example in the excel. Here is the screenshot of the data and the output for the table.

enter image description here

2 Answers 2

1

You could use the windowing variant of max:

SELECT id, date, MAX(date) OVER (PARTITION BY id)
FROM   mytable
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this might work:

WITH maxdts AS (
  SELECT id, max(dt) maxdt FROM table GROUP BY id
)
SELECT id, date, maxdt FROM table t, maxdts m WHERE t.id = m.id;

Keep in mind without more information that this could be a horribly inefficient query, but it will get you what you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.