ID Username ModifiedDate
1 A.BEENA 2009-12-07 04:48:17.980
2 A.BEENA 2009-11-17 06:02:27.443
3 Abhilash 2009-12-07 04:48:17.980
4 abhilash.r 2009-12-07 04:48:17.980
5 AJI 2009-11-17 06:02:27.443
6 haris 2009-12-07 04:48:17.980
7 haris 2009-11-17 06:02:27.443
我想通过ModifiedDate选择所有不同用户的详细信息。
我需要这样的输出
1 A.BEENA 2009-12-07 04:48:17.980
3 Abhilash 2009-12-07 04:48:17.980
4 abhilash.r 2009-12-07 04:48:17.980
5 AJI 2009-11-17 06:02:27.443
6 haris 2009-12-07 04:48:17.980
请帮帮我
答案 0 :(得分:7)
使用以下查询:
WITH CTE (DuplicateCount,Username,ModifiedDate) AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY Username ORDER BY ModifiedDate DESC) AS DuplicateCount,
Username,
ModifiedDate
FROM YourTable
)
SELECT *
FROM CTE
WHERE DuplicateCount = 1
答案 1 :(得分:3)
SELECT Username, MAX(ModifiedDate) AS LastModified
FROM MyTable
GROUP BY Username
这将提供您提及的输出,该输出似乎显示每个用户名的最新日期。
答案 2 :(得分:0)
你走了:
CREATE TABLE #temp(ID int, Username varchar(50), ModifiedDate datetime)
INSERT INTO #temp(ID, Username, ModifiedDate)
VALUES
(1, 'A.BEENA', '2009-12-07 04:48:17.980'),
(2, 'A.BEENA', '2009-11-17 06:02:27.443'),
(3, 'Abhilash', '2009-12-07 04:48:17.980'),
(4, 'abhilash.r', '2009-12-07 04:48:17.980'),
(5, 'AJI', '2009-11-17 06:02:27.443'),
(6, 'haris', '2009-12-07 04:48:17.980'),
(7, 'haris', '2009-11-17 06:02:27.443')
SELECT t.Username, t.ModifiedDate
FROM #temp t
WHERE NOT EXISTS (SELECT 1 FROM #temp WHERE Username = t.Username AND ModifiedDate > t.ModifiedDate)