Can someone please help me to understand this query. It works and I get results, however, what I really would like help with is a breakdown of the SQL statements and what they do exactly. I have to start creating these types of queries but I am at an absolute loss as to what they mean or how to put them together.
  DECLARE 
 @StartDate Datetime = '2014-11-01'
 , @EndDate DateTime = '2014-12-01'
 
 SELECT 
 PatientProfile.PatientId
 , PatientProfile.searchname as PatientName
 , HistoricalInfo.ChangeDate RespProviderSince
 , DoctorFacility.ListName RespProvider
 , DoctorFacility_1.ListName LastRespProvider
 
 INTO #LastResp
 
 FROM   
 CentricityPS.dbo.PatientProfile PatientProfile
	INNER JOIN CentricityPS.dbo.HistoricalInfo HistoricalInfo
		ON HistoricalInfo.PatientProfileId=PatientProfile.PatientProfileId
		AND HistoricalInfo.ChangeDate =
			(SELECT MAX(ChangeDate)
				FROM HistoricalInfo hi
					WHERE hi.PatientProfileId = HistoricalInfo.PatientProfileId
			)
	INNER JOIN CentricityPS.dbo.DoctorFacility DoctorFacility_1 
		ON HistoricalInfo.DoctorId=DoctorFacility_1.DoctorFacilityId
	INNER JOIN CentricityPS.dbo.DoctorFacility DoctorFacility 
		ON PatientProfile.DoctorId=DoctorFacility.DoctorFacilityId
 WHERE  PatientProfile.searchname NOT  LIKE '<MRG>%' 
 AND HistoricalInfo.DoctorId<>-1 
 AND HistoricalInfo.WhatChange=262144
 ORDER BY PatientProfile.PatientId
  
 SELECT DISTINCT 
 PatientProfile.PatientId
 , PatientProfile.searchname as PatientName
 , Appointments.ApptStart
 , ApptType.Name as VisitType
 , DoctorFacility_1.ListName as VisitProvider
 , DoctorFacility.ListName as PCP
 
 INTO #Appts
 
 FROM   
 CentricityPS.dbo.PatientProfile PatientProfile 
	LEFT JOIN CentricityPS.dbo.DoctorFacility DoctorFacility 
		ON PatientProfile.DoctorId=DoctorFacility.DoctorFacilityId
	LEFT JOIN CentricityPS.dbo.Appointments Appointments 
		ON PatientProfile.PatientProfileId=Appointments.OwnerId
	LEFT JOIN CentricityPS.dbo.ApptType ApptType 
		ON Appointments.ApptTypeId=ApptType.ApptTypeId
	LEFT JOIN CentricityPS.dbo.DoctorFacility DoctorFacility_1 
		ON Appointments.ResourceId=DoctorFacility_1.DoctorFacilityId
	LEFT JOIN CentricityPS.dbo.MedLists ApptStatus 
		ON Appointments.ApptStatusMId=ApptStatus.MedListsId
	LEFT JOIN CentricityPS.dbo.DoctorFacility DoctorFacility_2 
		ON Appointments.FacilityId=DoctorFacility_2.DoctorFacilityId
	LEFT JOIN #LastResp lr
		ON lr.PatientId = PatientProfile.PatientId
 WHERE  Appointments.ApptStart>= @StartDate
	AND Appointments.ApptStart< @EndDate
	AND Appointments.Status<>'Scheduled' 
	AND ApptStatus.Description IN 
		(
		'Arrived'
		,'Check Out'
		,'Completed'
		,'EMR Duplicate'
		,'Waiting for Provider'
		,'Waiting Room'
		,'With Provider'
		) 
	AND DoctorFacility_2.ListName IN 
		(
		'Family Practice - Gloucester' 
		,'Family Practice - Peabody' 
		,'Family Practice - Salem'
		) 
	AND  NOT ApptType.Name IN
		(
		'Behavioral Health - 30' 
		,'Behavioral Health 45' 
		,'Behavioral Health Initial - 30' 
		,'Fin Counsel - 15' 
		,'Fin Counsel - 30' 
		,'Fin Counsel 45 min' 
		,'Finacial Couinseling 45' 
		,'Financial Counseling' 
		,'Financial Counseling 45' 
		,'Financial Counseling Peabody 45' 
		,'Financial Counselor Family Visit' 
		,'Follow up Financial Counselors' 
		,'Lab Visit - 15' 
		,'Nurse Booked Visit - 15' 
		,'Nurse Booked Visit - 20' 
		,'Nurse Booked Visit - 30' 
		,'Nurse Booked Visit - 40' 
		,'Nurse Only Book - 15' 
		,'Nurse Only Book - 20' 
		,'Nurse Visit - 15' 
		,'Nurse Visit - 30' 
		,'Nurse Visit Only' 
		,'sbx visit'
		)
	AND (lr.RespProviderSince IS NULL OR Appointments.ApptStart >= lr.RespProviderSince) --either no change, or change in PCP was before the encounter
 ORDER BY DoctorFacility.ListName, PatientProfile.PatientId, Appointments.ApptStart DESC
 
 
 
 
 SELECT 
 VisitProvider 
 , CAST(SUM(CASE WHEN PCP = VisitProvider THEN 1 ELSE 0 END) AS FLOAT) / COUNT(ApptStart) as PercentVisitWithPCP
 , SUM(CASE WHEN PCP = VisitProvider THEN 1 ELSE 0 END) as VisitsWithPCP
 , COUNT(ApptStart) as TotalVisits
 
 from #Appts
 GROUP BY VisitProvider
 HAVING (SUM(CASE WHEN PCP = VisitProvider THEN 1 ELSE 0 END) > 0)
 ORDER BY VisitProvider
 
 DROP TABLE #Appts, #LastResp
Thank you.