I'm not sure how to figure this one out, but here's a sample table:
╔════════════╦════════╦═══════════╗
║ Company_ID ║ Status ║ Timestamp ║
╠════════════╬════════╬═══════════╣
║ 1234 ║ Test ║ 4/1/15 ║
║ 1234 ║ Live ║ 3/30/15 ║
║ 3456 ║ Live ║ 1/30/15 ║
║ 4567 ║ Test ║ 2/12/15 ║
║ 3456 ║ Test ║ 4/15/15 ║
╚════════════╩════════╩═══════════╝
I'd want to pull just the latest timestamp, so I would just want:
╔════════════╦════════╦═══════════╗
║ Company_ID ║ Status ║ Timestamp ║
╠════════════╬════════╬═══════════╣
║ 1234 ║ Test ║ 4/1/15 ║
║ 4567 ║ Test ║ 2/12/15 ║
║ 3456 ║ Test ║ 4/15/15 ║
╚════════════╩════════╩═══════════╝
Thus far, I want to try this, but I'm afraid it'd just pull the largest timestamp, but not with the associated status. Is this right?
select
company_id,
status,
max(timestamp)
from
sample
group by 1, 2
EDIT: It's on Redshift (PostgreSQL).