我正在尝试创建一个仅显示最新 CoverageGroup
的报告,包括null值。
样本 - 当前与想要
这是当前代码(没有max()函数):
SELECT
p.AcctNumber,
c.sDate,
ISNULL(cv.Coveragegroup,0) AS [CoverageGroup],
wd.Code,
ISNULL(CO2.lname,'Patient') AS [PrimIns],
p.DefaultCoverageGroup,
ISNULL(cv.LevelOfCoverage,0) AS [LevelOfCoverage]
FROM
Patient p
FULL OUTER JOIN
Charge c ON c.PatientID = p.IDPatient
FULL OUTER JOIN
Coverage cv ON cv.PatientiD = p.IDPatient
FULL OUTER JOIN
WorkDescriptor wd ON wd.IDWorkdescriptor = c.WorkDescriptorID
FULL OUTER JOIN
InsCompany ic on ic.IDInsCompany = cv.InsCompanyID
FULL OUTER JOIN
Contact co2 on co2.IDContact = ic.ContactID
WHERE
p.AcctNumber IN (256500, 256569)
AND (cv.Coveragegroup = p.DefaultCoverageGroup OR p.DefaultCoverageGroup = 0)
AND (cv.LevelOfCoverage = 1 OR cv.LevelOfCoverage IS NULL)
ORDER BY
p.AcctNumber, c.sDate
我已经尝试了MAX(COALESCE...
和MAX(CASE WHEN...
的不同变体,但它们无法使用,或者它们不会以Null CoverageGroup
显示帐户。
知道我该怎么做?我在SQL上的专业知识是初学者。
谢谢!
也许是因为这个:
AND (cv.Coveragegroup = p.DefaultCoverageGroup OR p.DefaultCoverageGroup = 0)
在这里您实际上指定不允许它为NULL
。
您可以通过将其更改为这样的东西来避免它:
AND (cv.Coveragegroup = p.DefaultCoverageGroup OR p.DefaultCoverageGroup = 0 OR cv.Coveragegroup IS NULL)