Skip to content

Instantly share code, notes, and snippets.

@shivrajbadu
Last active December 6, 2019 05:22
Show Gist options
  • Select an option

  • Save shivrajbadu/2f9d2dc2a1799f36f53467376528f33d to your computer and use it in GitHub Desktop.

Select an option

Save shivrajbadu/2f9d2dc2a1799f36f53467376528f33d to your computer and use it in GitHub Desktop.
ignore duplicates
res = Patient.find_by_sql(%{
select id from (
select
patients.id as id,
patient_histories.year as year,
patient_histories.month as month,
patient_histories.day as day
from patients
join patient_histories
on patients.id=patient_histories.patient_id
where patients.gender="female"
and patient_histories.year = '2076'
group by
patients.id,
patient_histories.year,
patient_histories.month,
patient_histories.day
having count(patient_histories.patient_id) = 1
)t group by id
having (count(*) = 1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment