SQL某人最后一条使用日期记录。

 DECLARE @TB TABLE([用户ID] VARCHAR(5), [使用日期] DATETIME, [使用时间] DATETIME, [有效期] INT, [消费点数] INT)
INSERT @TB
SELECT '10001', '2009-02-01  10:21:32', '2010-01-31', 0, 1000 UNION ALL
SELECT '10001', '2009-02-03  18:30:15', '2010-01-31', 50, 950 UNION ALL
SELECT '10001', '2009-03-01  14:06:28', '2010-01-31', 60, 890 UNION ALL
SELECT '10001', '2009-03-01  16:12:08', '2010-01-31', 40, 850 UNION ALL
SELECT '10002', '2009-04-01  10:21:32', '2010-03-31', 0, 500 UNION ALL
SELECT '10002', '2009-04-10  20:15:02', '2010-03-31', 80, 420 UNION ALL
SELECT '10002', '2009-04-20  17:26:18', '2010-03-31', 20, 400 UNION ALL
SELECT '10003', '2009-02-01  10:21:32', '2009-01-31', 0, 200


SELECT * FROM @TB T
WHERE NOT EXISTS(SELECT 1 FROM @TB WHERE [用户ID]=T.[用户ID] AND [使用日期]>T.[使用日期])

你可能感兴趣的:(sql,table,insert,2010)