日期:2014-05-17  浏览次数:20432 次

各位大神,分组取最大记录啊

GO 
IF OBJECT_ID('test_table') IS NOT NULL
DROP TABLE test_table

CREATE TABLE test_table
(
[sid] INT PRIMARY KEY IDENTITY(1,1),
[email] NVARCHAR(50),
[imei] NVARCHAR(50),
[lastseen] DATETIME
)

GO
INSERT INTO test_table
SELECT 'aaaa@163.com', '000000000000000', '2013-01-14 04:41:12.000'
UNION ALL
SELECT 'bbbb@163.com', '000000000000000', '2013-01-13 22:22:30.000'
UNION ALL
SELECT 'cccc@163.com', '000000000000000', '2013-01-13 04:03:13.000'
UNION ALL
SELECT 'dddd@163.com', '6f52a254806809263ef9c426fbf77e890b173887', '2013-01-12 14:07:31.000'
UNION ALL
SELECT 'eee@163.com', '6f52a254806809263ef9c426fbf77e890b173887', '2013-01-11 15:05:04.000'
UNION ALL
SELECT 'ffff@163.com', '868655000382418', '2013-01-13 17:01:48.000'
UNION ALL
SELECT 'gggg@163.com', '868655000382418', '2013-01-12 18:29:45.000'


SELECT * FROM test_table

/*
sid         email                                              imei                                               lastseen
----------- -------------------------------------------------- -------------------------------------------------- -----------------------
1           aaaa@163.com                                       000000000000000                                    2013-01-14 04:41:12.000
2           bbbb@163.com                                       000000000000000                                    2013-01-13 22:22:30.000
3           cccc@163.com                                       000000000000000                                    2013-01-13 04:03:13.000
4           dddd@163.com                &