Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Find Patients last appointment location

Avatar of ozzy t
ozzy t asked on
Microsoft SQL ServerSQL
3 Comments1 Solution97 ViewsLast Modified:
I am trying to determine the patients last appointment location, everything is fine until i add "location" field then i produce more results,since a patient more than likely has been treated in more than one location in area, i am stuck on how to go about this, how would i go about this? i am guessing i could try to tie last appointment date to location, i just don't know how. Thanks for any help or suggestions.

    

select distinct
medicare_tbl.Patient_Number,
Patient_First_Name,
Patient_Last_Name,
Patient_DOB,
Usual_Dr_Name,
MAX(Service_Date_From) AS Last_Appointment

FROM 
(
	SELECT     distinct PM.vwGenPatInfo.Patient_Number,Patient_First_Name,Patient_Last_Name,Patient_DOB,Usual_Dr_Name,Service_Date_From,Location_Abbr
	FROM         PM.vwGenPatInfo LEFT JOIN
						  PM.vwGenSvcInfo ON PM.vwGenPatInfo.Patient_Number = PM.vwGenSvcInfo.Patient_Number
	WHERE     (PM.vwGenPatInfo.Prim_Policy_Carrier_Name IN ('MEDICARE PART B', 'MEDICARE UGS')) 
	AND PM.vwGenSvcInfo.Service_Date_From >= CONVERT(DATE, DATEADD(MONTH, -24, GETDATE()))
) as medicare_tbl 


INNER JOIN

(
	SELECT     distinct PM.vwGenPatInfo.Patient_Number
	FROM         PM.vwGenPatInfo LEFT JOIN
						  PM.vwGenSvcInfo ON PM.vwGenPatInfo.Patient_Number = PM.vwGenSvcInfo.Patient_Number
	WHERE  
	
	(
		(
			PM.vwGenPatInfo.PrimM_Policy_Carrier_Reporting_Class_Abbr in ('Table95a', 'Table95b', 'Table94') 
			AND PM.vwGenPatInfo.SecM_Policy_Carrier_Reporting_Class_Abbr in ('Table92a', 'Table92b', 'Table91')
		)
		OR
		(
			PM.vwGenPatInfo.PrimM_Policy_Carrier_Reporting_Class_Abbr in ('Table92a', 'Table92b', 'Table91') 
			AND PM.vwGenPatInfo.SecM_Policy_Carrier_Reporting_Class_Abbr in ('Table95a', 'Table95b', 'Table94')
		)
	)
	AND PM.vwGenSvcInfo.Service_Date_From >= CONVERT(DATE, DATEADD(MONTH, -24, GETDATE()))
) as dually_eligible_tbl on medicare_tbl.Patient_Number = dually_eligible_tbl.Patient_Number




GROUP BY 
medicare_tbl.Patient_Number,
Patient_First_Name,
Patient_Last_Name,
Patient_DOB,
Usual_Dr_Name
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike EghtebasFlag of United States of America imageDatabase and Application Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers