日期:2014-05-18  浏览次数:20489 次

面试题,求每天最后一个到公司的人,求解。
ID Name Date Time
1 A 2012/06/11 09:08
1 A 2012/06/11 09:10
1 A 2012/06/11 09:10
1 A 2012/06/11 09:12
1 A 2012/06/12 09:15
1 A 2012/06/12 09:19
2 B 2012/06/11 09:13
2 B 2012/06/11 09:22
2 B 2012/06/12 09:16
2 B 2012/06/12 09:39

这是一张考勤表,求每天最后一个到公司的人。假设表名为SignIn.

------解决方案--------------------
SQL code

select * from test a
where a.Time=(select max(Time) from test b where a.Date    =b.Date)

------解决方案--------------------
SQL code
--#1.
select 
    A.*,
    B.name,
    B.ID
from
(
    select
        Date = convert(char(10), Date, 120),
        Time = max(Time)
    from SignIn
    group by convert(char(10), Date, 120)
) A
inner join SignIn B
    on A.Date = B.Date
        and A.Time = B.time
        
--#2.
select B.* from
(select distinct [Date] from SignIn) A
cross apply
(select top(1) * from SignIn where [Date] = A.[Date] order by [Time] desc) B