Skip to content

Instantly share code, notes, and snippets.

@mikehins
Last active December 19, 2022 14:15
Show Gist options
  • Save mikehins/ba43c5872847f22e0e126eb91eaa0481 to your computer and use it in GitHub Desktop.
Save mikehins/ba43c5872847f22e0e126eb91eaa0481 to your computer and use it in GitHub Desktop.
SELECT participants.*,
RANK() over ( ORDER BY total_time ) rank,
(SELECT SUM(time) FROM `leaderboards` WHERE `participant_id` = `event_participant`.`participant_id` and `event_id` = `event_participant`.`event_id` LIMIT 1) as `total_time`,
`event_participant`.`event_id` as `pivot_event_id`, `event_participant`.`participant_id` as `pivot_participant_id`
FROM `participants`
inner join `event_participant` on `participants`.`id` = `event_participant`.`participant_id`
WHERE `event_participant`.`event_id` in (29) ORDER BY `total_time` ASC
$participants = Participant::select('participants.*',
DB::raw('RANK() OVER (ORDER BY total_time) rank'),
DB::raw('(SELECT SUM(time) FROM leaderboards WHERE participant_id = event_participant.participant_id and event_id = event_participant.event_id LIMIT 1) as total_time'),
'event_participant.event_id as pivot_event_id',
'event_participant.participant_id as pivot_participant_id'
)
->join('event_participant', 'participants.id', '=', 'event_participant.participant_id')
->whereIn('event_participant.event_id', [29])
->orderBy('total_time', 'asc')
->get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment