最后输入日期,不论主题如何

时间:2011-10-19 13:49:40

标签: sql tsql

先谢谢你的帮助, 我有一个查询,appsolutly工作正常,但我的要求是找到学生最后一个入学日期,无论主题

非常感谢

WITH T_Subjects AS 
(  
SELECT T_Student_History.StudenID,  
Convert(varchar(11), MAX(T_Student_History.Created),101)AS 'Student Last entry',  
'History' AS 'Subject Name'   
FROM T_Student_History   
Group BY T_Student_History.StudentID  
UNION ALL  

SELECT T_Student_geography.StudentID,  
Convert(varchar(11), MAX(T_Student_geography.Created),101)AS 'Student Last entry',  
'Geography' AS 'Subject Name'   
FROM T_Student_geography  
Group BY T_Student_geography.StudentID  
Union ALL  

SELECT T_Student_Computing.StudentID,  
Convert(varchar(11), MAX(T_Student_Computing.Created),101)AS 'Student Last entry',  
'Computing' AS 'Subject Name'   
FROM  T_Student_Computing  
Group BY  T_Student_Computing.StudentID  
) 

SELECT   
T_Students.StudentID 
T_Students.Forename+' '+T_Students.Surname AS 'Student Description',                   
T_Students.Email, 
T_Students.Mobile, 
T_Subjects.StudentLastEntry  
T_Subjects.Subject Name 
FROM T_Students  
LEFT OUTER JOIN T_Subjects ON T_Students.StudentID = T_Subjects.StudentID 

3 个答案:

答案 0 :(得分:3)

我认为下面的查询会为您提供所需内容。如果有多名学生参加最后一次参赛,那么它将返回所有学生。我修复了一些小问题,然后添加了最后几行WHERE子句。

    WITH  T_Subjects
        AS ( SELECT  T_Student_History.StudentID ,  --Typo here?
                     MAX(T_Student_History.Created) AS [Student Last entry] ,
                     'History' AS [Subject Name]
             FROM    T_Student_History
             GROUP BY T_Student_History.StudentID
             UNION ALL
             SELECT  T_Student_geography.StudentID ,
                     MAX(T_Student_geography.Created) AS [Student Last entry] ,
                     'Geography' AS [Subject Name]
             FROM    T_Student_geography
             GROUP BY T_Student_geography.StudentID
             UNION ALL
             SELECT  T_Student_Computing.StudentID ,
                     MAX(T_Student_Computing.Created) AS [Student Last entry] ,
                     'Computing' AS [Subject Name]
             FROM    T_Student_Computing
             GROUP BY T_Student_Computing.StudentID
           )
   SELECT   T_Students.StudentID ,
            T_Students.Forename + ' ' + T_Students.Surname AS [Student Description] ,
            T_Students.Email ,
            T_Students.Mobile ,
            CONVERT(VARCHAR(11), T_Subjects.[Student Last Entry], 101) AS [Student Last Entry],
          -- Do you really want the above as a String and not a date?
            T_Subjects.[Student Last Entry] ,
            T_Subjects.[Subject Name]
   FROM     T_Students
   LEFT OUTER JOIN T_Subjects
            ON T_Students.StudentID = T_Subjects.StudentID
   WHERE    T_Subjects.[Student Last Entry] = ( SELECT MAX([Student Last Entry])
                                                FROM     T_Subjects ts2
                                                WHERE    ts2.StudentID = T_Students.StudentID
                                          ) 

答案 1 :(得分:3)

这对ranking window functions很有用。

假设您已经或多或少地定义了T_Subject

  SELECT StudentID,
         MAX(Created) AS [Created],
         'Whatever' AS [Subject Name]
    FROM T_Student_Whatever
GROUP BY StudentID
   UNION ALL
  SELECT ... and so on for each T_Student_Foo...

然后,您可以通过以下方式获取每个StudentID的最新科目注册(您将其称为“学生最后一个条目”):

SELECT StudentID,
       Created,
       [Subject Name]
  FROM (SELECT T_Subjects.*,
               ROW_NUMBER() OVER (PARTITION BY StudentID ORDER BY Created DESC)
                 AS [ranking] -- rank subject enrollment by recency
          FROM T_Subjects)
 WHERE ranking = 1            -- now select only the first ranked per StudentID

然后,您可以使用T_Students表加入 查询,以获取姓名和手机号码等。

更新2011-10-20

目前还不完全清楚OP正在寻找什么,但RANK()而不是ROW_NUMBER()可能更受欢迎。前者将重复等效分区特征的排名,即记录可以“排列”第N个位置。

例如,假设ID#1的学生最近最近同时参加了计算和历史,但是学生#2最近在两周前参加了计算,差异将是:

[ RANK()d ]
 StudentID | StudentLastEntry | SubjectName
-----------+------------------+-------------
         1 |       2011-10-19 | Computing
         1 |       2011-10-19 | History
         2 |       2011-10-06 | Computing

与例如

[ ROW_NUMBER()d ]
 StudentID | StudentLastEntry | SubjectName
-----------+------------------+-------------
         1 |       2011-10-19 | History       -- or 'Computing'
         2 |       2011-10-06 | Computing

答案 2 :(得分:2)

更简单,你可以使用MAX的Over()。在最后的SELECT中尝试这个

MAX(T_Subjects.StudentLastEntry) 
    OVER (PARTITION BY T_Subjects.StudentID) AS StudentLastEntry

很多人都知道在排名函数中使用OVER和PARTITION BY,但你也可以将它与MAX和COUNT等其他聚合一起使用。