我有以下查詢獲取具有給定條件記錄的計數。以下顯示了表中的兩條記錄和查詢結果。我想要的是根據EntryDate
列獲得最新的事件。sql找到最近的記錄
0 A 2012-08-09 00:00:00.000 N0206527 N0168639 NULL 09 420202 LTD False
0 A 2012-08-08 00:00:00.000 N0206527 N0168639 NULL 09 420202 LTD False
DCMNumber CustomerDivision CustomerSerial Product PriorNonDenied
N0064819 09 420202 LTD 2
SET @firstDayOfCurrentMonth = DATEADD(m,DATEDIFF(M,0,GETDATE()),0)
SELECT DCMNumber, CustomerDivision, CustomerSerial, Product, COUNT(*) AS PriorNonDenied
FROM cauAssignedClaim
WHERE RecordType = 'A' AND
EscFromSTD = '0' AND
([Status] <> 'DE' OR
[Status] IS NULL) AND
CONVERT(VARCHAR(10), EntryDate, 110) BETWEEN
CASE WHEN Product = 'LTD' THEN CONVERT(VARCHAR(10), @firstDayOfCurrentMonth, 110)
ELSE CONVERT(VARCHAR(10), DATEADD(Day, -7, GETDATE()-1), 110)
END
AND CONVERT(VARCHAR(10), GETDATE()-1, 110)
GROUP BY [Status],
DCMNumber,
CustomerDivision,
CustomerSerial,
Product
感謝, 布拉德
您正在使用哪種RDBMS? – Yuck 2012-08-13 13:16:55
似乎它是SQL Server – codingbiz 2012-08-13 13:18:59
看起來像MSSQL – 2012-08-13 13:19:26