我一直在努力过滤以仅显示 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