Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Complex Query Help

Author  Topic 

mikeallenbrown
Yak Posting Veteran

72 Posts

Posted - 2015-04-13 : 16:42:48
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 needed


SELECT 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.CompanyName
FROM PatientLocation pl LEFT OUTER JOIN
contact LocContact ON pl.ContactID = LocContact.id
ORDER 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 Brown
ITOT Solutions, Inc.
SQL Server 2012
Alpha Five v3 (12)

Kristen
Test

22859 Posts

Posted - 2015-04-14 : 04:35:33
There are tidier ways to do it, but this should work:

Add this JOIN

JOIN
(
SELECT [T_RowNumber] = ROW_NUMBER()
OVER
(
PARTITION BY PKey1, PKey2, ...
ORDER BY PKey1, PKey2, ..., ID4
),
PKey1,
PKey2,
...
FROM PatientLocation
) AS X
ON X.T_RowNumber = 1 -- FIRST row for each patient only
AND X.PKey1 = P.PKey1
AND X.PKey2 = P.PKey2
AND ...
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2015-04-14 : 04:37:56
An improvement would be to replace the [Patient] table with that sub-query, you just need to include any columns needed in the rest of the query (in addition to the PKey1, PEKy2, ...) listed in the SELECT of the sub-query, and change the Alias Name from [X] to [P]
Go to Top of Page
   

- Advertisement -