[英]SQL to get max date with group by
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. 从这张表中我必须找出一个学生和主题id最近回答的行。
The SQL I am using .. 我正在使用的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
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 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 should come once with recent timestamp only.... 3004应该只有一个最近的时间戳只....
Can any one help me out with the proper SQL... 任何人都可以帮助我正确的SQL ...
I am using Oracle RDS in AWS 我在AWS中使用Oracle RDS
for this is better use ROW_NUMBER()
为此更好用
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
You appears to want : 你似乎想要:
select e.*
from exam_trans e
where Insert_DTM = (select max(e1.Insert_DTM)
from exam_trans e1
where e1.Subject_id = e.Subject_id and
e1.Question_id = e.Question_id
);
You can use sub-query and row_number(). 您可以使用子查询和row_number()。
https://stackoverflow.com/a/10515397/7328059 should answer your question. https://stackoverflow.com/a/10515397/7328059应该回答你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.