Skip to content

Instantly share code, notes, and snippets.

@stevencwarren
Created January 9, 2013 22:17
Show Gist options
  • Save stevencwarren/4497503 to your computer and use it in GitHub Desktop.
Save stevencwarren/4497503 to your computer and use it in GitHub Desktop.
Select outcomes, inverted_outcomes, sum(outcomes,inverted_outcomes) as goals_met
FROM
(SELECT Count(outcome_records) FROM "visits"
INNER JOIN progress_reports ON progress_reports.visit_id = visits.id
INNER JOIN outcome_records ON progress_reports.id = outcome_records.progress_report_id
INNER JOIN outcome_goals ON outcome_records.goal_id = outcome_goals.id
WHERE ends_on IS NOT NULL
AND outcome_records.integer_value IS NOT null
AND progress_reports.final is true
AND outcome_goals.inverted is false
AND outcome_records.integer_value >= outcome_goals.goal) as outcomes,
(SELECT Count(outcome_records) FROM "visits"
INNER JOIN progress_reports ON progress_reports.visit_id = visits.id
INNER JOIN outcome_records ON progress_reports.id = outcome_records.progress_report_id
INNER JOIN outcome_goals ON outcome_records.goal_id = outcome_goals.id
WHERE ends_on IS NOT NULL
AND outcome_records.integer_value IS NOT null
AND progress_reports.final is true
AND outcome_goals.inverted is true
AND outcome_records.integer_value <= outcome_goals.goal) as inverted_outcomes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment