SQL Query以排除满足特定要求的任何项目(占空值)

时间:2017-04-25 19:12:58

标签: sql intersystems-cache

我看到类似的问题,但没有一个能够完全回答这个问题的问题,所以我会继续问。

我需要选择一组设施,但是一旦他们匹配了一组标准,他们可能被包括在内。因此,如果提交月份与特定月份匹配且FacWideAU,FacWideAR或AU = 1(非0或null),则不能包含该工具。这些设施可能有多种结果。

Sample DB Example (only Hosp1 should qualify):

 HospID  |  HospName        |        FacWideAU   |  FacWideAR  |  AU
 ---------------------------------------------------------------
 1          Hosp1                    0
 2          Hosp2                    1             0            0
 2          Hosp2                    0                          0
 3          Hosp3                    1             1            0
 3          Hosp3                   
 3          Hosp3                    0

是否有一种更简单的方式来编写查询而不是以下(重复所有选择逻辑,它具有连接并且实际上更复杂)?

非常感谢!

   Select distinct HospID, HospName
      from <Hospital Table>
      where Description='...whatever...'
      etc.

   and ID NOT in
   (
      Select distinct HospID 
         from <Hospital Table>
         where Description='...whatever...'
         etc.

         and (case when (TO_CHAR(SubmissionMonth, 'MON-YYYY') = 'Mar-2017' AND
         (FacWideAU = 1 or FacWideAR = 1 or AU = 1)) then 1 else 0 end) = 1
   )

3 个答案:

答案 0 :(得分:1)

我找到了另一种解决方案:您可以在桌面上使用左连接。

Select distinct h1.HospID, h1.HospName
      from <Hospital Table> h1
      left join <Hospital Table> h2 on h1.HospId = h2.HospId
            and (((TO_CHAR(h2.SubmissionMonth, 'MON-YYYY') = 'Mar-2017')
            and (isnull(h2.FacWideAU, 0) = 1 or isnull(h2.FacWideAR, 0) = 1 or isnull(h2.AU, 0) = 1))

      where Description='...whatever...'
      and h2.id is null
      etc.

答案 1 :(得分:0)

只需为每列添加空检查:

public class ScreenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {    

        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            intent = new Intent(context, Login2.class);
            context.startActivity(intent);               
        }
    }
}

答案 2 :(得分:0)

您可以放弃not in部分并更改原始where部分:

where Description='...whatever...'
   and ((TO_CHAR(SubmissionMonth, 'MON-YYYY') != 'Mar-2017')
   or (FacWideAU IS NULL 
   and FacWideAU = 0 
   and FacWideAR IS NULL
   and FacWideAR = 0
   and AU IS NULL
   and AU = 0))