我有一张如下表:
DECLARE @tmp TABLE
(
userID int,
testID int,
someDate datetime
)
在其中我存储日期以及两个 ID 值,例如
INSERT INTO @tmp (userID, testID, someDate) VALUES (1, 50, '2010-10-01')
INSERT INTO @tmp (userID, testID, someDate) VALUES (1, 50, '2010-11-01')
INSERT INTO @tmp (userID, testID, someDate) VALUES (1, 50, '2010-12-01')
INSERT INTO @tmp (userID, testID, someDate) VALUES (2, 20, '2010-10-01')
INSERT INTO @tmp (userID, testID, someDate) VALUES (2, 20, '2010-11-01')
我需要为每个 userID/testID 组合选择最新日期。所以,结果将是
userID testID someDate
1 50 2010-12-01
2 20 2010-11-01
这听起来很容易,但我无法弄清楚。SQL 小提琴在这里。