如何筛选2列中包含NOT NULLS的行

时间:2013-11-15 05:10:52

标签: sql filter null multiple-columns aliases

我一直在努力过滤,只显示2列中没有NULLS的行。

  • 我只想要have a value in both Disability 1 and Disability 2的客户(行)的结果。

  • 不希望显示have a value in Disability 1 and NULL in Disability 2 or a NULL in Disability 1 and value in Disability 2的客户(行)。

我的专栏是ClientName, ClientNumber, ClientAge, ClientGender, Disability1, Disability2

Disability列是使用CASE的别名。

我发现的问题:

  • 无法在Where或Having子句中引用别名,这使得难以过滤。

  • Having子句引用D.DiagnosisName为2个条件,这意味着我没有返回任何结果。

不胜感激。

干杯

我的代码:

SELECT 
    C.ClientName, 
    C.ClientNumber, 
    C.ClientAge, 
    C.ClientGender,
    CASE 
        WHEN    D.DiagnosisName = 'Depression' 
                OR D.DiagnosisName = 'Anxiety' 
                OR D.DiagnosisName = 'Drug and Alcohol' 
                OR D.DiagnosisName = 'Bipolar Disorder' 
                OR D.DiagnosisName = 'Delusional' 
                OR D.DiagnosisName = 'Eating Disorder' 
                OR D.DiagnosisName = 'Obsessive Compulsive Disorder' 
                OR D.DiagnosisName = 'Personality Disorder' 
                OR D.DiagnosisName = 'Post Traumatic Stress Disorder' 
                OR D.DiagnosisName = 'Post-Natal' 
                OR D.DiagnosisName = 'Schizo-affective disorder' 
                OR D.DiagnosisName = 'Schizophrenia' 
                OR D.DiagnosisName = 'Other Psychiatric/Psychological Disorder' 
                OR D.DiagnosisName = 'BPD' 
                OR D.DiagnosisName = 'Psychiatric (other)' 
            THEN D.DiagnosisName 
    END AS [Disability1],
    CASE 
        WHEN    D.DiagnosisName = 'Autism' 
                OR D.DiagnosisName = 'Intellectual' 
                OR D.DiagnosisName = 'Specific Learning/ADD' 
                OR D.DiagnosisName = 'Acquired Brain Injury / Head Injury' 
                OR D.DiagnosisName = ' Acquired Brain Injury-Head Injury' 
                OR D.DiagnosisName = 'Neurological' 
        THEN D.DiagnosisName 
    END AS [Disability2]

FROM 
    dbo.FACTClientDiagnosis R
    RIGHT OUTER JOIN DimClient C ON R.DimClientID = C.DimClientID
    LEFT OUTER JOIN DimDiagnosisType D ON R.DimDiagnosisTypeID = D.DimDiagnosisTypeID

WHERE 
    ClientStatus = 'Active'     
    AND ClientType = 'Client'
    AND D.DiagnosisName NOT LIKE 'NULL'

GROUP BY 
    C.ClientName, 
    C.ClientNumber, 
    C.ClientAge, 
    C.ClientGender, 
    D.DiagnosisName

HAVING 
    (
        D.DiagnosisName = 'Depression' 
        OR D.DiagnosisName = 'Anxiety' 
        OR D.DiagnosisName = 'Drug and Alcohol' 
        OR D.DiagnosisName = 'Bipolar Disorder' 
        OR D.DiagnosisName = 'Delusional' 
        OR D.DiagnosisName = 'Eating Disorder' 
        OR D.DiagnosisName = 'Obsessive Compulsive Disorder' 
        OR D.DiagnosisName = 'Personality Disorder' 
        OR D.DiagnosisName = 'Post Traumatic Stress Disorder' 
        OR D.DiagnosisName = 'Post-Natal' 
        OR D.DiagnosisName = 'Schizo-affective disorder' 
        OR D.DiagnosisName = 'Schizophrenia' 
        OR D.DiagnosisName = 'Other Psychiatric/Psychological Disorder' 
        OR D.DiagnosisName = 'BPD' 
        OR D.DiagnosisName = 'Psychiatric (other)'
    )
    AND 
    (
        D.DiagnosisName = 'Autism' 
        OR D.DiagnosisName = 'Intellectual' 
        OR D.DiagnosisName = 'Specific Learning/ADD' 
        OR D.DiagnosisName = 'Acquired Brain Injury / Head Injury' 
        OR D.DiagnosisName = ' Acquired Brain Injury-Head Injury' 
        OR D.DiagnosisName = 'Neurological'
    ) 

ORDER BY 
    C.ClientName

1 个答案:

答案 0 :(得分:1)

您可以通过将其包装在另一个SELECT .. FROM中来过滤派生/别名列,如下所示。此外,我无法理解为什么在这种情况下您需要HAVING - 过滤器可以移动到WHERE,因为您没有聚合过滤器。

SELECT ..., x.Disability1, x.Disability2, ...
FROM
(
    SELECT ... AS Disability1,
           ... AS Disability2
    FROM ...
    WHERE ...
    GROUP BY ...
) AS x
WHERE x.Disability1 IS NOT NULL AND x.Disability2 IS NOT NULL;

其他一些潜在问题

  • 您可以将重复的OR替换为IN ('Intellectual', 'Autism', ...)
  • 您有一个过滤器(x in (a,b,c))... AND x IN (d,e,f) - 除非有重叠,否则不会返回任何记录。
  • 使用IS NULLIS NOT NULL检查空值,而不是D.DiagnosisName NOT LIKE 'NULL'