I have a transaction table something like this ...
-------------------------------------------------------------------------------------
Subject_id | Question_id | Answerd_id | Student_id | Answer_text | Insert_DTM
-------------------------------------------------------------------------------------
5005 | 3004 | 1004 | 1309 | test | 2018-05-31 12:07:42
-------------------------------------------------------------------------------------
5005 | 3005 | 1005 | 1309 | test | 2018-05-31 12:07:42
-------------------------------------------------------------------------------------
5005 | 3004 | NULL | 1309 | Null | 2018-05-31 12:09:43
-------------------------------------------------------------------------------------
5005 | 3002 | NULL | 1309 | Null | 2018-05-31 12:07:42
-------------------------------------------------------------------------------------
5005 | 3005 | 1005 | 1309 | test | 2018-05-31 11:35:47
-------------------------------------------------------------------------------------
5005 | 3005 | 1005 | 1309 | |2018-05-31 11:36:37
From this table I have to find out most recent answered row for a student and subject id.
The SQL I am using ..
SELECT subject_id,
question_id,
answer_id,
student_id,
answer_text,
insert_dtm
FROM exam_trans
WHERE student_id = 1309
AND subject_id = 5005
AND insert_dtm IN (SELECT Max(insert_dtm)
FROM exam_trans
WHERE student_id = 1309
AND subject_id = 5005
GROUP BY question_id)
GROUP BY subject_id,
question_id,
answer_id,
member_id,
answer_text,
insert_dtm
ORDER BY answer_id,
question_id DESC
But As output I am getting
-------------------------------------------------------------------------------------
Subject_id | Question_id | Answerd_id | Student_id | Answer_text | Insert_DTM
-------------------------------------------------------------------------------------
5005 | 3004 | 1004 | 1309 | test | 2018-05-31 12:07:42
-------------------------------------------------------------------------------------
5005 | 3005 | 1005 | 1309 | test | 2018-05-31 12:07:42
---------------------------------------------------------------------------------------
5005 | 3004 | NULL | 1309 | Null | 2018-05-31 12:09:43
--------------------------------------------------------------------------------------
5005 | 3002 | NULL | 1309 | Null | 2018-05-31 12:07:42
3004 is appearing twice in the output , but my expected output is
Subject_id | Question_id | Answerd_id | Student_id | Answer_text | Insert_DTM
-------------------------------------------------------------------------------------
5005 | 3005 | 1005 | 1309 | test | 2018-05-31 12:07:42
---------------------------------------------------------------------------------------
5005 | 3004 | NULL | 1309 | Null | 2018-05-31 12:09:43
--------------------------------------------------------------------------------------
5005 | 3002 | NULL | 1309 | Null | 2018-05-31 12:07:42
3004 should come once with recent timestamp only....
Can any one help me out with the proper SQL...
I am using Oracle RDS in AWS