- select distinct on (sysname) sysname, snapshot, stage, git_head_ref
- from build_status
- where sysname = ? and branch = ? and snapshot < ?
- order by sysname, snapshot desc limit 1
+ select git_head_ref
+ from build_status
+ where sysname = ? and branch = ? and snapshot =
+ (select max(snapshot)
+ from build_status
+ where sysname = ? and branch = ? and snapshot < ?)