根据ID按日期顺序检索不同的记录

时间:2016-03-14 10:04:27

标签: sql sql-server

我有一个问题:

composer update

返回结果如下。

select C.ChapterID, C.ChapterName, TA.TestAllotmentID, 
       T.TestName, S.StudentFname, B.BatchName, TA.UpdatedDate 
from TransTestAllotment TA,
     MstStudent S,
     MstBatchDetails B,
     MstTest T,
     MstChapter C
where TA.StudentID = 47
  and TA.BatchID = 10 
  and T.TestID = TA.TestID
  and S.StudentID = TA.StudentID
  and B.BatchID = TA.BatchID
  and T.ChapterID = C.ChapterID
  and TA.IsAttempted = 'True'
  and TA.IsEvaluated = 'True'
order by TA.UpdatedDate desc

现在我想根据更新日期的章节ID顺序检索不同的记录。我期待的最终输出是:

+-----------+-----------------------+-----------------+-------------------------+
| ChapterID |      ChapterName      | TestAllotmentID |       UpdatedDate       |
+-----------+-----------------------+-----------------+-------------------------+
|        52 | HTML Basics - Part II |              37 | 2016-03-14 13:12:53.000 |
|        52 | HTML Basics - Part II |              36 | 2016-03-14 13:11:59.000 |
|        52 | HTML Basics - Part II |              35 | 2016-03-14 13:11:35.000 |
|        50 | HTML Basics - Part I  |              28 | 2016-03-11 18:45:51.000 |
|        50 | HTML Basics - Part I  |              27 | 2016-03-11 18:45:05.000 |
|        50 | HTML Basics - Part I  |              26 | 2016-03-11 18:43:48.000 |
|        50 | HTML Basics - Part I  |              24 | 2016-03-11 18:42:37.000 |
|        50 | HTML Basics - Part I  |              22 | 2016-03-11 18:41:07.000 |
+-----------+-----------------------+-----------------+-------------------------+

如何编写SQL查询以获取此输出?

4 个答案:

答案 0 :(得分:2)

您应该修复查询以使用正确的显式JOIN语法。但你的问题的答案是使用窗口函数:

with q as (
      <your query here>
     )
select q.*
from (select q.*,
             row_number() over (partition by chapterid order by updateddate desc) as seqnum
      from q
     ) q
where seqnum = 1;

答案 1 :(得分:2)

谢谢Gordon Linoff。我的最终查询如下。

Select Y.* from (select X.*, row_number() over (partition by chapterid order by updateddate desc) as SequencNo from 
    (Select C.ChapterID,C.ChapterName,TA.TestAllotmentID, T.TestName,
    S.StudentFname,B.BatchName,TA.UpdatedDate
    from TransTestAllotment TA, MstStudent S, MstBatchDetails B, 
    MstTest T,MstChapter C 
    where TA.StudentID=47 and TA.BatchID=10 and 
    T.TestID=TA.TestID and S.StudentID=TA.StudentID and 
    B.BatchID=TA.BatchID and T.ChapterID=C.ChapterID and 
    TA.IsAttempted='True' and TA.IsEvaluated='True') X) Y Where SequencNo=1

答案 2 :(得分:0)

我建议使用JOIN重构查询,然后查看是否需要进一步的子句:

这是一次尝试

SELECT 
C.ChapterID, 
C.ChapterName,
FROM 
(SELECT
TA.TestAllotmentID, 
T.TestName, 
S.StudentFname, 
B.BatchName, 
TA.UpdatedDate 
FROM TransTestAllotment TA
LEFT JOIN MstStudent S ON S.StudentID=TA.StudentID
LEFT JOIN MstBatchDetails B ON B.BatchID=TA.BatchID
LEFT JOIN MstTest T ON T.TestID=TA.TestID
WHERE TA.StudentID=47
AND TA.BatchID=10 as T1
AND TA.IsAttempted='True'
AND TA.IsEvaluated='True'
ORDER BY TA.UpdatedDate desc)
LEFT JOIN MstChapter C ON  T1.ChapterID=C.ChapterID;

此致

答案 3 :(得分:0)

你可以尝试这个查询:

select C.ChapterID, C.ChapterName, TA.TestAllotmentID, TA.UpdatedDate 
    from TransTestAllotment TA,
         MstStudent S,
         MstBatchDetails B,
         MstTest T,
         MstChapter C
    where TA.StudentID = 47
      and TA.BatchID = 10 
      and T.TestID = TA.TestID
      and S.StudentID = TA.StudentID
      and B.BatchID = TA.BatchID
      and T.ChapterID = C.ChapterID
      and TA.IsAttempted = 'True'
      and TA.IsEvaluated = 'True'
     group by  C.ChapterID, C.ChapterName, TA.TestAllotmentID, TA.UpdatedDate  
     having  (C.ChapterID,TA.TestAllotmentID) in 
     (select C1.ChapterID ,max(TA1.TestAllotmentID)  
        from TransTestAllotment TA1, MstTest T1, MstChapter C1 
        where  T1.TestID = TA1.TestID and T1.ChapterID = C1.ChapterID)
     order by TA.UpdatedDate desc