I've tried for two days to get this right but it's just way over my head...This is my current query I am using and it worked for what I neededSELECT p.ClientID, p.StartOfCare, PhyContact.CombinedName AS PhyName, p.Address, p.City, p.State, p.Zip, PhyContact.TaxonomyCode AS PhyTaxID, PhyContact.NPI AS PhyNPI, contact.CompanyName AS InsuranceName, PIns.InsuranceNumber, pl.Locationid AS Facility, p.County AS PatientCounty, p.DOB, p.Sex AS Gender, p.eightyoneA, Client.ClientName, Client.Address AS ClientAddress, Client.City AS ClientCity, Client.State AS ClientState, Client.Zip AS ClientZip, Client.PhoneNumber AS ClientPhone, Client.FaxNumber AS ClientFax, Client.NPI AS ClientNPI, Client.FEIN AS ClientTaxID, Client.CCN AS PTAN, p.ID, p.MRNum, p.Compined, p.PrimDiag, pl.Location4, pl.NPI AS FacilityNPI, LocContact.CompanyName FROM (Patient p LEFT OUTER JOIN (PatientLocation pl LEFT OUTER JOIN contact LocContact ON pl.ContactID = LocContact.id ) ON pl.Patientid4 = p.ID LEFT OUTER JOIN (PatientPhysician Pphy LEFT OUTER JOIN contact PhyContact ON Pphy.PhysicianID = PhyContact.id ) ON Pphy.PatientID = p.ID AND Pphy.Role = 'Attending' LEFT OUTER JOIN Client Client ON p.ClientID = Client.ID LEFT OUTER JOIN (PatientInsurance PIns LEFT OUTER JOIN contact contact ON PIns.InsuranceID = contact.id ) ON pIns.PatientID = p.ID AND pIns.PrimaryOrSecondary = 'Primary' )
The problem I recently discovered is that patients p often have multiple locations pl. I only want the most recent patient location to be included in the query. Here is a query that returns the most recent patient location...SELECT TOP(1) pl.Patientid4, pl.ContactID, pl.ID4, pl.Locationid, pl.Location4, pl.NPI, LocContact.CompanyNameFROM PatientLocation pl LEFT OUTER JOIN contact LocContact ON pl.ContactID = LocContact.idORDER BY pl.ID4 DESC
I don't know how to work that second query into the first...that is what I need help with. I hope that wasn't too convoluted.Mike BrownITOT Solutions, Inc.SQL Server 2012Alpha Five v3 (12)