Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save shivrajbadu/d5918bf2c57caf0dede82d0096d44c95 to your computer and use it in GitHub Desktop.
this is the simple query which return all new visited patients on filter date
select patient_histories.*, patients.* from patients join patient_histories on patient_histories.id = (
select id from patient_histories
where patient_histories.patient_id = patients.id
order by patient_histories.id asc
limit 1
)
/*where patients.gender="female"*/
/*and patient_histories.year="2076" and patient_histories.month="8" and patient_histories.day="19"*/
/*https://www.periscopedata.com/blog/4-ways-to-join-only-the-first-row-in-sql)*/
def self.qry
year = "2076"
month = "4"
day = "1"
p=[]
patients = Patient.all
patients.each do |patient|
first_patient_history = patient.patient_histories.first
p << patient if first_patient_history.year == year && first_patient_history.month==month && first_patient_history.day==day
end
puts p.size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment