0

我一直在努力过滤以仅显示 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 或 Have 子句中引用别名,这使得过滤变得困难。

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

将不胜感激任何指针。

干杯

我的代码:

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
4

1 回答 1

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的 s替换为IN ('Intellectual', 'Autism', ...)
  • 您有一个过滤器(x in (a,b,c))... AND x IN (d,e,f)- 除非有重叠,否则不会返回任何记录。
  • IS NULL用andIS NOT NULL和 not检查空值D.DiagnosisName NOT LIKE 'NULL'
于 2013-11-15T05:15:11.763 回答