Skip to content

Instantly share code, notes, and snippets.

@jaylevitt
Created March 23, 2012 01:25
Show Gist options
  • Save jaylevitt/2166021 to your computer and use it in GitHub Desktop.
Save jaylevitt/2166021 to your computer and use it in GitHub Desktop.
tappables sort by time query
SELECT tappables.id, max(
case
when tappables.type = 'SurveyReceipt'
then tappables.created_at
when taps.user_id = 2816
then taps.created_at
else
taps.skewed_created_at
end) as last_tap
FROM tappables
left join taps on taps.tappable_id = tappables.id
where tappables.type != 'SurveyReceipt'
group by tappables.id
order by last_tap desc
Table "public.taps"
Column | Type | Modifiers
-------------------+-----------------------------+----------------------------------------------------
id | integer | not null default nextval('taps_id_seq1'::regclass)
tappable_id | integer |
user_id | integer |
created_at | timestamp without time zone |
updated_at | timestamp without time zone |
original | boolean |
source | text |
skewed_created_at | timestamp without time zone |
Indexes:
"taps_pkey1" PRIMARY KEY, btree (id)
"taps_created_at_idx" btree (created_at)
"taps_skewed_created_at_idx" btree (skewed_created_at)
"taps_tappable_id_idx" btree (tappable_id)
"taps_user_id_idx" btree (user_id)
Foreign-key constraints:
"taps_clip_id_fk" FOREIGN KEY (tappable_id) REFERENCES tappables(id)
"taps_user_id_fk" FOREIGN KEY (user_id) REFERENCES users(id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment