表中前 1 个不同的行

发布于 2024-11-04 09:51:34 字数 635 浏览 0 评论 0原文

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

请帮助我

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

I want to select details of all distinct users order by ModifiedDate.

I need output like this

   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

Please help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱暴击 2024-11-11 09:51:34

使用以下查询:

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

Use the following query:

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
羅雙樹 2024-11-11 09:51:34
SELECT Username, MAX(ModifiedDate) AS LastModified
FROM MyTable
GROUP BY Username

这将给出您提到的输出,该输出似乎显示每个用户名的最新日期。

SELECT Username, MAX(ModifiedDate) AS LastModified
FROM MyTable
GROUP BY Username

This will give the output you mention, which appears to show the most recent date for each username.

-黛色若梦 2024-11-11 09:51:34

干得好:

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)

Here you go:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文