Skip to content

Instantly share code, notes, and snippets.

@jeremywrnr
Last active August 29, 2015 14:07
Show Gist options
  • Save jeremywrnr/4191017026efbfc3a37d to your computer and use it in GitHub Desktop.
Save jeremywrnr/4191017026efbfc3a37d to your computer and use it in GitHub Desktop.
some fun sql query highlights
CREATE OR REPLACE FUNCTION heartrate()
RETURNS TABLE(nodeid integer, hrbpm numeric) AS
$BODY$
DECLARE r record;
BEGIN
FOR r IN SELECT DISTINCT wampum.nodeid FROM wampum ORDER BY nodeid
LOOP
RETURN QUERY SELECT wampum.nodeid, wampum.hrbpm FROM wampum
WHERE wampum.nodeid=r.nodeid ORDER BY id DESC LIMIT 1;
END LOOP;
RETURN;
END
$BODY$
LANGUAGE 'plpgsql' ;
with early as (
select sid, min(timestamp)
from thinkcspy_log, courses
where courses.course_name = thinkcspy_log.course_id
and courses.inst_type = 'highschool'
group by sid
)
select thinkcspy_log.sid, toc.chapter_id
from early, thinkcspy_log, toc
where early.sid = thinkcspy_log.sid
and early.min = thinkcspy_log.timestamp
and toc.div_id = thinkcspy_log.div_id;
select sid, min(timestamp), max(timestamp)
from thinkcspy_log, courses
where courses.course_name = thinkcspy_log.course_id
and courses.inst_type = 'highschool'
group by sid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment