select id,action from logs A inner join 
(select ID,max(Occur ) as Occur  from logs group by ID) B 
on A.ID=B.ID and A.Occur=B.Occur

解决方案 »

  1.   

    OR:
    create table logs(ID int, Occur datetime, Action tinyint)
    go
    insert into logs
    select 1,'2005-1-1 12:00:00' ,1
    union select 2,'2005-1-2 13:00:00',      2
    union select 1,'2005-1-2 13:00:00',      2
    union select 4,'2005-1-3 12:00:00',      1
    union select 4,'2005-1-3 13:00:00',      2
    union select 4,'2005-1-3 14:00:00',      3
    ----查询
    select * from logs A where not exists(select * from logs where id=A.id and Occur>A.Occur )
    ------结果
    ID          Occur                                                  Action 
    ----------- ------------------------------------------------------ ------ 
    1           2005-01-02 13:00:00.000                                2
    2           2005-01-02 13:00:00.000                                2
    4           2005-01-03 14:00:00.000                                3(所影响的行数为 3 行)