problem: we have millions of patient visits and we want to get all last patients visits in the hospital
solution: 1-we have to separate them by patient id wise to avoid engine crushing
2- we will use group by in our query BUT we should group by only patient id
***trick*** to bring more details in your query and avoid redundancy , then you may add more columns but in the form of calculations then you don't have to put them after group by
the below example brings max visit of each patient ..and i have added VISIT_DATE as well in my details without adding it after group by ....just by using max(v.VISIT_DATE)
select max(v.VISIT_NO) , v.PATIENT_ID ,max(v.VISIT_DATE) as last_visit
from OPS_PATIENT_VISITS v
where v.PATIENT_ID between 1 and 10
group by v.PATIENT_ID
Comments
Post a Comment