sql server - Fetching records from multiple tables -


i have 3 tables in database given below. have records these 3 tables such rf_date should between startdate , end date supplying query , page_id should equal pagetype supply query , 1 last assigned_to. suppose sent " 10/10/2003 12:00:00 am" start date , 10/10/2015 12:00:00 end date , pagetype=1 3 records table tblfrontusers each record have multiple entires in second table i.e "tblpatient", need latest "assigned_to" each user. eg: 115,latest there "5" , 116, latest "1" , 5 , 1 have it's names stored in last table i.e tblusers. trying following query finding difficult desired results. please help.

    select tblfrontusers .name, tblfrontusers .id, tblfrontusers.page_id, tblpatient.patient_id , tblusers.name            tblpatient.assigned_to   tblfrontusers left join tblpatient on tblpatient .id=tblfrontusers .id   left join tblusers on tblusers.id = tblpatient .assigned_to     tblfrontusers .rf_date >= '12/17/2003 12:00:00 am'      ,   tblfrontusers .rf_date<='3/21/2014 12:00:00 am' , tblfrontusers.page_id=1      , there can search column tblpatient.assigned_to also. if present need records otherwise all.      tblfrontusers   id         name           rf_date                         page_id 115        sandy          10/10/2013 12:00:00            1 116        javer          10/25/2013 12:00:00            1 117        felipe         10/22/2013 12:00:00            1  tblpatient  patient_id      id        assigned_to 1               115         2 2               115         3 3               115         5 4               116         5 5               116         3 6               116         1  tblusers id              name        1               max 2               sam 3               harry 4               mary         5               maria 

select * tblfrontusers fo left join (select max(patient_id) patient_id,id tblpatient             group id)p on p.id=fo.id inner join tblusers u on u.id=(select assigned_to tblpatient p1 p1.patient_id=p.patient_id) (fo.rf_date between '12/17/2003 12:00:00 am' , '3/21/2014 12:00:00 am'  , fo.page_id=1) order fo.[id] 

sql fiddle

update

select * tblfrontusers fo left join (select max(patient_id) patient_id,id tblpatient             group id)p on p.id=fo.id left join tblpatient p1 on p1.patient_id=p.patient_id left join tblusers u on u.id=p1.assigned_to (fo.rf_date between '12/17/2003 12:00:00 am' , '3/21/2014 12:00:00 am'  , fo.page_id=1) --and p1.assigned_to='2' order fo.[id] 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -