1

I have a table with isin (test), date (Date), and amount (int).

I am trying to get the amount of the last 2 dates per isin.

Someone can tell me the trick please?

Rick James
80.7k5 gold badges52 silver badges119 bronze badges
asked Apr 10, 2024 at 20:36
2
  • Last 2 dates are first 2 dates when the ordering is reversed. Commented Apr 11, 2024 at 4:25
  • See the tag I added or Groupwise-Max Commented Apr 27, 2024 at 4:29

1 Answer 1

4

With MySQL 8 you can use a CTE with then Window function ROW_NUMBER to get your wanted data

WITH CTE AS(SELECT
isin , date , amount ,
ROW_NUMBER() OVER(PARTITION BY isin ORDER BY date DESC) rn
FROM mytable)
SELECT 
isin , date , amount
FROM CTE
WHERE rn <= 2
ORDER BY isin, rn
answered Apr 10, 2024 at 21:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.