如果第一个值为NULL,如何选择下一个值

时间:2019-11-01 15:34:04

标签: sql sql-server subquery

我试图编写那些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

我将需要代码从顶部1移至下一个值。Order子句中的RowNum是使用上一个表中的分区创建的行号,按现状,它等于1。是使用此Select Top 1方法的许多Select子查询之一。我正在使用这种方法从查询中获取最新信息,但是如果需要,我们可以依靠下一个最新数据。

这是一些示例输出。整个输出很大,有很多列。

  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

2 个答案:

答案 0 :(得分:1)

要解释我的想法,请执行以下简单查询

SELECT 
    (SELECT TOP 1 t.name from sys.tables T 
                WHERE 1 = 0 
                AND t.name IS NOT NULL 
                order by T.name
    ) DQ

t.name可能永远不会为NULL,而且我永远也不会选择它,但是无论如何我都不会选择行,因此您会看到NULL-子查询不返回任何内容,NULL

第二个查询显示有0行,而不是查询正在获取NULL字段

    SELECT 
    (SELECT  COUNT(t.name) from sys.tables T 
                WHERE 1 = 0 
                AND t.name IS NOT NULL 

    ) DQ

答案 1 :(得分:0)

inner join (SELECT [Prenatal status], RECORD_ID, RowNum
            FROM #TmpPrenatalEnrollPAT
            WHERE RowNum < 4
           ) PreNatalEnroll on PreNatalEnroll.RECORD_ID = ClientSVC.SVC_ID