I want to fetch 3 latest devices for a person. Table structure is shown below
PERSON_DEVICE
person_id seq_num device_detail device_detail_added
999 1 Nexus 03/04/2015
999 2 iphone 06/07/2015
999 3 laptop 08/09/2015
999 4 uda 02/01/2016
999 5 pda 04/04/2016
1001 1 sm 03/03/2015
... and so on
For latest device for a person I can use following query
select * from PERSON_DEVICE
where person_id = 999
and seq_num = (select max(seq_num) from PERSON_DEVICE where person_id = 999)
but for second latest and third latest, I don`t know how to get this ?
I tried using limit
, but in Sybase it is not working (for my Sybase version).
P.S. seq_num is used to identify latest device for a particular person id.
********************EDIT*****************************************88
PERSON
person_id name addres
999 john 2 avenue
1001 elliot rrt
I want to records to
select pn.name , pd.device1 ,pd.device_detail_added1, pd.device2 ,pd.device_detail_added2,pd.device3 ,pd.device_detail_added3 from PERSON pn , PERSON_DEVICE pd