如何在SQL Server中使用AttTime从两行数据中查找单行记录

时间:2011-10-10 12:26:58

标签: sql sql-server-2005 tsql

我有查询使用SQL Server 2005查找使用First AttTime和Last AttTime的两行中的单个记录的记录

我的AttLog表结构是:

EnrollNo    Int
AttDate     DateTime
AttMonth    Int
AttDay      Int
AttYear     Int
AttTime     Varchar

当前输出:

EnrollNo     AttDate               AttMonth   AttDay   AttYear   AttTime
405  2011-03-09 09:59:00.000       9          3        2011      9:59     
405  2011-03-09 18:40:00.000       9          3           2011   18:40  

期望的输出:

EnrollNo     AttDate               AttMonth   AttDay   AttYear   FirstTime   Last Time  
405  2011-03-09 09:59:00.000        9        3          2011      9:59        18:40

1 个答案:

答案 0 :(得分:0)

假设您使用相同的EnrollNo对记录进行分组,则应使用aggregate functions

  SELECT EnrollNo, Min(AttDate), Min(AttMonth), Min(AttDay), Min(AttYear), Min(AttTime) as FirstTime, Max(AttTime) as LastTime    
  FROM AttLog
  GROUP BY EnrollNo