我试图弄清楚如何在 where 子句中使用 case 语句,以便根据产品类型列选择特定条件。where 子句的第一部分对这两种情况都是通用的。如果产品类型是 STD,我需要检查 EntryDate 是否在当月的第一天和昨天之间。如果产品是 LTD,那么我需要检查 EntryDate 是否在昨天和 7 天前之间。
DECLARE @firstDayOfCurrentMonth datetime
SET @firstDayOfCurrentMonth = CAST(CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '/' + CAST(MONTH(GETDATE()) AS VARCHAR(2)) + '/01' AS DATETIME)
SELECT DCMNumber, COUNT(*) AS PriorDenied
FROM cauAssignedClaim
WHERE RecordType = 'A' AND [Status] IS NULL AND
CASE WHEN Product = 'LTD' THEN EntryDate BETWEEN @firstDayOfCurrentMonth AND GETDATE()-1
CASE WHEN Product = 'STD' THEN EntryDate BETWEEN DATEADD(Day, -7, GETDATE()-1) AND GETDATE()-1
END
GROUP BY [Status], DCMNumber