我有以下表结构。我想根据 office_id 和最新的 login_datetime 选择不同的 user_id。
tbl_id user_id office_id login_datetime
----------------------------------------
1 2 28 12/28/2012 5:35:50 AM
2 2 15 12/28/2012 5:35:50 AM
3 3 20 12/28/2012 5:35:50 AM
4 4 28 12/28/2012 5:35:50 AM
5 2 28 12/28/2012 5:35:50 AM
6 4 15 12/28/2012 5:35:50 AM
7 3 20 12/28/2012 5:35:50 AM
我试过了:
SELECT user_id as u_id,office_id,
(select login_datetime from tbl t2 where t2.user_id=u_id AND ROWNUM=1 ORDER BY t2.tbl_id DESC ) as LAST_LOGIN
FROM tbl
GROUP BY user_id,office_id
但是,它对我不起作用,有什么帮助吗?