我一直在努力过滤,只显示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
答案 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 NULL
和IS NOT NULL
检查空值,而不是D.DiagnosisName NOT LIKE 'NULL'