[英]SQL to get max date with group by
我有一個像這樣的交易表......
-------------------------------------------------------------------------------------
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
為此更好用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
你似乎想要:
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
);
您可以使用子查詢和row_number()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.