如果Trimester值为'FirstVisit1HealthCenter'或'FirstVisit1OtherProvider',我需要将Trimester列重命名为'FirstTri'。如果Trimester列中的值为'FirstVisit2HealthCenter'或'FirstVisit2OtherProvider',则Trimester列为'SecondTri'。与具有“3”的行相同。该列将是'ThirdTri'。
“首次访问健康中心”列中的值不为空,需要返回这些值。 “非首次访问其他提供者”列中的值也不需要保留。
这是我拥有的SELECT
语句,但它不断返回NULL
SELECT DISTINCT 6 + ROW_NUMBER() OVER (ORDER BY Trimester) AS Line,
CASE WHEN Trimester IN ('FirstVisit1HealthCenter', 'FirstVisit1OtherProvider') THEN 'First Trimester'
WHEN Trimester IN ('FirstVisit2HealthCenter', 'FirstVisit2OtherProvider') THEN 'Second Trimester'
WHEN Trimester IN ('FirstVisit3HealthCenter', 'FirstVisit3OtherProvider') THEN 'Third Trimester' END AS EarlyEntry,
CASE WHEN [First Visit with Health Center] IS NOT NULL THEN [First Visit with Health Center] END AS [First Visit with Health Center],
CASE WHEN [First Visit with Other Provider] IS NOT NULL THEN [First Visit with Other Provider] END AS [First Visit with Other Provider]
--SELECT *
FROM #tblSubResult
如何 NOT 返回NULL但只返回3行的行?
答案 0 :(得分:1)
您可以将查询作为子查询并对列求和:
select EarlyEntry,
sum([First Visit with Health Center]) as [First Visit with Health Center],
sum([First Visit with Other Provider]) as [First Visit with Other Provider]
from (
SELECT DISTINCT 6 + ROW_NUMBER() OVER (ORDER BY Trimester) AS Line,
CASE WHEN Trimester IN ('FirstVisit1HealthCenter', 'FirstVisit1OtherProvider') THEN 'First Trimester'
WHEN Trimester IN ('FirstVisit2HealthCenter', 'FirstVisit2OtherProvider') THEN 'Second Trimester'
WHEN Trimester IN ('FirstVisit3HealthCenter', 'FirstVisit3OtherProvider') THEN 'Third Trimester' END AS EarlyEntry,
CASE WHEN [First Visit with Health Center] IS NOT NULL THEN [First Visit with Health Center] END AS [First Visit with Health Center],
CASE WHEN [First Visit with Other Provider] IS NOT NULL THEN [First Visit with Other Provider] END AS [First Visit with Other Provider]
--SELECT *
FROM #tblSubResult) a
group by EarlyEntry