Skip to content

Instantly share code, notes, and snippets.

@jaylevitt
Created March 19, 2012 22:51
Show Gist options
  • Save jaylevitt/2127909 to your computer and use it in GitHub Desktop.
Save jaylevitt/2127909 to your computer and use it in GitHub Desktop.
SELECT tappables.id,
max(case
when tappables.type = 'Survey'
then case
when (not exists survey_responses) -- pseudo-code
then now() - random() * interval '1 day'
else
max(survey_responses.created_at)
end
when taps.user_id = :user_id
then taps.created_at
else
taps.skewed_created_at
end) as last_tap
FROM tappables
join taps on taps.tappable_id = tappables.id
join survey_responses as sr
on sr.survey_id = tappables.id
and sr.user_id = :user_id
group by tappables.id
order by last_tap desc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment