As you see above question id 25 appeared more than once. This is basically a quiz result table where for users as they answered. question_id 25 always the first questions. Any user can go though the quiz N number of time. So, I want to find the last occurrence of the question_id 25 for any specific user in the table, and select that and all answers the users gave after this till the end of the quiz. Any idea how to solve it in a single efficient query. My all try didn't work out.
WITH quest AS (SELECT id, question_id, user_id FROM questions WHERE user_id = 5142670086 --> substitute any user_id value AND question_id = 25 --> substitute any question_id value ORDER BY 1, 2, 3) SELECT * FROM quest WHERE id IN (SELECT max(id) FROM quest);
--
Melvin Davidson I reserve the right to fantasize. Whether or not you wish to share my fantasy is entirely up to you.