I am trying to figure out how to use a case statement in a where clause in order to select specific criteria based on the Product type column. The first part of the where clause is common to both scenarios. If the Product type is STD, I need to check to see if the EntryDate is between the firs day of the current month and yesterday. If the Product is LTD, then I need to check to see if the EntryDate is between yesterday and seven days ago.
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
You want
That’s assuming this is valid syntax since I’ve never tried it 🙂
I prefer this since it’s quite concise – if you format it I suppose it’s quite readable too