Created
March 19, 2012 22:51
-
-
Save jaylevitt/2127909 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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