我有一个像这样的交易表......
-------------------------------------------------------------------------------------
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
从这张表中我必须找出最近一个学生和主题ID的答案行。
我正在使用的SQL ..
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
但是作为输出我正在
-------------------------------------------------------------------------------------
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在输出中出现两次,但我的预期输出是
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应该只带一个最近的时间戳....
任何人都可以帮助我使用正确的SQL ......
我在AWS中使用Oracle RDS
答案 0 :(得分:2)
这样可以更好地使用ROW_NUMBER()
SELECT *
FROM (SELECT *,
ROW_NUMBER() OVER (PARTION BY Question_id, Subject_id
ORDER BY Insert_DTM DESC) as rn
FROM exam_trans
WHERE student_id = 1309
AND subject_id = 5005
)
WHERE rn = 1
答案 1 :(得分:1)
您似乎想要:
report-status-and-split turtle [[who] of myself]
答案 2 :(得分:0)
您可以使用子查询和row_number()。