with record as(
SELECT player_id, giftbag_id, status, timestamp,ROW_NUMBER() OVER (PARTITION BY player_id ORDER BY timestamp DESC) AS rn from mafia1.status_snapshot
where
timestamp >='2019-10-21'--and timestamp <='2019-11-22'
order by player_id, timestamp desc),
charge_record as(
select * from record
where giftbag_id is not null and rn <=2),
charge_record1 as(
select charge_record.*, record.status as status_1, record.timestamp as timestamp_1, record.giftbag_id as giftbag_id_1, record.rn as rn_1 from (charge_record left join record
on charge_record.player_id = record.player_id and charge_record.rn = record.rn -1))
select *from(
select *from(select charge_record1.*,record.status as status_2,record.timestamp as timestamp_2, record.giftbag_id as giftbag_id_2, record.rn as rn_2 from (charge_record1 left join record
on charge_record1.player_id = record.player_id and charge_record1.rn = record.rn -2)))
order by player_id, rn