Я пытаюсь написать эти подзапросы Select, чтобы, если значение Top 1 было NULL, оно переместилось бы к следующему значению, отличному от NULL. Вот код:
----------------- GATHERING DATE DATA FROM ALL 12 MONTH UDF'S INTO TEMP TABLE #tmpAll12MonthForms -----------------
select e.RECORD_ID,e.FORM_INSTANCE_ID,e.FORM_NAME,e.[FIELD_VALUE] as [DATE_FORM_COMPLETED],
row_number() over (partition by record_id order by cast([FIELD_VALUE] as date) desc) as RowNum into #tmpAll12MonthForms
from [View_CMMode_UDFData] e
where e.FORM_DEF_DR IN
('HMFPAT12mPostEn','HMFPAT24mPostEn','HMFPAT36mPostEn'
,'HMFPAT48mPostEn','HMFPAT60mPostEn'
,'HMFPAT72mPostEn','HMFPATEnroll','PATEnroll'
,'HMFPATEnrollV01','SCMTmechvEnroll','HMFSCEnroll','HMFNFP12mPostEn'
,'HMFNFP24mPostEn','HMFNFPEnroll')
AND e.FIELD_DEF_DR in ('PATMIECHV12mPostEn','PATMIECHV24mPostEn'
,'PATMIECHV12mPostEnGuideDtVstFrmComp'
,'PATMIECHV24mPostEnGuideDtVstFrmComp'
,'PATMIECHV36mPostEnGuideDtVstFrmComp'
,'MTmechv48mPostEnGuideDt','MTmechv60mPostEnGuideDt'
,'MTmechv72mPostEnGuideDt' ,'PATEnrollGuideDt','EnrollGuideDt'
,'FSMTmechv12mPostEn','FSMTmechv12mPostEnGuideDt'
,'NFPMIECHV12mUpdtGuideDt','NFPMIECHV18mUpdtGuideDt'
,'NFPMIECHV24mUpdtGuideDt','NFPMIECHV6mUpdtGuideDt'
,'NFPMIECHVEnrollGuideDt')
group by e.record_id,e.FORM_INSTANCE_ID,FORM_NAME,[FIELD_VALUE]
create unique clustered index All12MonthFormsU on #tmpAll12MonthForms (record_id,form_instance_id)
----------------- GATHERING PRENATAL ENROLLIES DATA FROM UDF INTO TEMP TABLE #tmpPrenatal -----------------
SELECT [RECORD_ID],[FORM_INSTANCE_ID],[field_value] as 'Prenatal status' INTO #tmpPrenatal
FROM [View_CMMode_UDFData]
where [SECTION_DEF_DR] in ('MTmechvEnrollInfo','FP') and [FIELD_DEF_DR] in ('MTmechvEnrollInfoChldPNEnroll','FPPG')
GROUP BY [RECORD_ID],[FORM_INSTANCE_ID],[field_value]
------------- COLLECTING CLIENT PRENATAL ENROLLMENT DATA FOR PAT --------------------
SELECT U.RECORD_ID,u.FORM_INSTANCE_ID,u.[Prenatal status],p.RowNum into #TmpPrenatalEnrollPAT
FROM #tmpPrenatal u inner join #tmpAll12MonthForms p
on p.RECORD_ID = u.RECORD_ID and p.FORM_INSTANCE_ID = u.FORM_INSTANCE_ID
group by U.RECORD_ID,u.FORM_INSTANCE_ID,u.[Prenatal status],p.RowNum
----------- THIS IS THE SELECT SUBQUERY I AM WORKING ON. THERE ARE MANY LIKE THIS ONE -------------
,(SELECT TOP 1 [Prenatal status]
FROM #TmpPrenatalEnrollPAT as prenatalEnroll WHERE prenatalEnroll.RECORD_ID = ClientSVC.SVC_ID
ORDER BY prenatalEnroll.RowNum asc
) AS PrenatalEnroll
............
............
INTO #TempPrimaryCGPAT
FROM #tmpClientServices AS ClientSVC
INNER JOIN #tmpClients AS Client ON Client.PER_ROWID = ClientSVC.SVC_PersonDR
SELECT .....................
,ClientSVC.PER_PRIMARYLANGUAGE as Client_PrimaryLanguage
,PrenatalEnroll as [Prenatally Enroll]
,ClientSVC.EnrollDate
,.............
into #TMPpatTable
FROM #TempPrimaryCGPAT AS ClientSVC
INNER JOIN E_Entity e (nolock) ON e.ID = ClientSVC.PrimaryCGID
INNER JOIN #tmpClients AS CAREGIVER ON CAREGIVER.PER_ROWID = e.EntityHx_ID
INNER JOIN #tmpLastHomeVisitPAT V ON V.RECORD_ID = ClientSVC.SVC_ID
Мне понадобится код, чтобы перейти к следующему значению из Top 1. RowNum в предложении Order - это номер строки, созданный с использованием раздела в предыдущей таблице. и как есть, он равен 1. Это один из многих подзапросов Select, использующих этот метод Select Top 1. Я использую этот метод для получения самой последней информации из запроса, но при необходимости мы можем обратиться к следующим самым последним данным.
Вот пример выходных данных. Весь вывод очень большой со многими столбцами.
Caregiver_MaritalStatus
Married
Not Married but Living Together with Partner
NULL
Not Married but Living Together with Partner
Never Married (not including not married but living with partner)
Not Married but Living Together with Partner
NULL
Married
Not Married but Living Together with Partner
Not Married but Living Together with Partner
Not Married but Living Together with Partner