I've referred How to get second largest or third largest entry from a table, but it only returns by looking through max value.
but here I need an sql query which uses max value of another field (here id which is a sequence).
Here is the table my_test
I need to get second largest unique value of sent_by, ie., 100 according through id
my query doesn't seems good, I've tried
SELECT sent_by
FROM MY_TEST
WHERE id =
(SELECT MAX(id)
FROM MY_TEST
WHERE id NOT IN
(SELECT id
FROM MY_TEST
WHERE sent_by =(SELECT sent_by FROM my_test WHERE id =(SELECT MAX(id) FROM MY_TEST))
)
); -- results 100
is there any other easy way to get the required? and how about if i need to get the 3rd?
editing
Here i'll update my question to make you more clear.
let us assume the given table is details of message transactions. let id be unique and is (sequence), sent_by is inserted with the individual's / person's id (like user_id). the sent_by column may / may not inserted with multiple rows with same user_id.
if i need to take latest sent_by value: i can query
select sent_by from my_test where id= (select max(id) from my_test);-- to get 60
my question is to take the second latest sent_by value: (in the given table ,
the 2nd latest sent_by value should be 100, as id 8, 9, 10 are same sent_by values
the 3rd latest sent_by value should be 4,
the 4th latest sent_by value should be 3
)
hope the question is clear now.
Thanks in advance:)