如何在 SQL 中根据 LastCreatedDate 选择不同的名称

发布于 2024-11-25 12:54:27 字数 1038 浏览 1 评论 0原文

我有包含列的示例表:

ID(guid)                                 Name        CreatedDate

B27EB95B-B219-46BD-9E72-A378B3E7FED0    A       2005-11-20 22:49:46.000
33D43825-918D-4DC0-874A-53670563EA03    A       2009-10-03 10:34:00.000
28F26DA6-B144-4C0C-AC2F-4DDD2D74357E    B       2011-03-23 08:08:39.000
1491D95F-BA58-41EE-8982-B713DE9DECD7    C       2006-01-18 14:53:12.000
FD25C05D-0C1A-4314-BCA7-F4F33B0E890D    C       2006-10-05 14:25:58.000
F4256300-CAA6-4E80-8D1B-B89273274088    D       2008-03-12 08:56:32.000

这应该是我在 SSIS 上的 OledbDatasource。

现在的问题是我需要获取具有最后创建日期的不同名称及其在sql中的ID。

这是输出结果:

ID(guid)                                 Name        CreatedDate

33D43825-918D-4DC0-874A-53670563EA03    A       2009-10-03 10:34:00.000
28F26DA6-B144-4C0C-AC2F-4DDD2D74357E    B       2011-03-23 08:08:39.000
047ED9C3-DF92-45D5-B295-EE52184116FB    C           2006-10-05 14:23:40.000
F4256300-CAA6-4E80-8D1B-B89273274088    D       2008-03-12 08:56:32.000

谢谢问候

I have sample table with columns:

ID(guid)                                 Name        CreatedDate

B27EB95B-B219-46BD-9E72-A378B3E7FED0    A       2005-11-20 22:49:46.000
33D43825-918D-4DC0-874A-53670563EA03    A       2009-10-03 10:34:00.000
28F26DA6-B144-4C0C-AC2F-4DDD2D74357E    B       2011-03-23 08:08:39.000
1491D95F-BA58-41EE-8982-B713DE9DECD7    C       2006-01-18 14:53:12.000
FD25C05D-0C1A-4314-BCA7-F4F33B0E890D    C       2006-10-05 14:25:58.000
F4256300-CAA6-4E80-8D1B-B89273274088    D       2008-03-12 08:56:32.000

That Supposed to be my OledbDatasource on SSIS.

Now problem is i need to get the distinct Name having the last createdDate and its Id in sql.

This is the output result:

ID(guid)                                 Name        CreatedDate

33D43825-918D-4DC0-874A-53670563EA03    A       2009-10-03 10:34:00.000
28F26DA6-B144-4C0C-AC2F-4DDD2D74357E    B       2011-03-23 08:08:39.000
047ED9C3-DF92-45D5-B295-EE52184116FB    C           2006-10-05 14:23:40.000
F4256300-CAA6-4E80-8D1B-B89273274088    D       2008-03-12 08:56:32.000

Thanks in Regards

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

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

发布评论

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

评论(4

栀子花开つ 2024-12-02 12:54:27

如果您使用的是 SQL Server 2005 或更高版本,则可以使用排名CTE

WITH ranked AS (
  SELECT
    ID, Name, CreatedDate,
    rn = ROW_NUMBER() OVER (PARTITION BY Name ORDER BY CreatedDate DESC)
  FROM atable
)
SELECT
  ID,
  Name,
  CreatedDate
FROM ranked
WHERE rn = 1

If you are on SQL Server 2005 or later, you can use ranking and a CTE:

WITH ranked AS (
  SELECT
    ID, Name, CreatedDate,
    rn = ROW_NUMBER() OVER (PARTITION BY Name ORDER BY CreatedDate DESC)
  FROM atable
)
SELECT
  ID,
  Name,
  CreatedDate
FROM ranked
WHERE rn = 1
心安伴我暖 2024-12-02 12:54:27
SELECT ID, Name, CreatedDate
FROM table JOIN
  (SELECT MAX(CreatedDate) CreatedDate, Name FROM table GROUP BY Name) max_date
USING (Name, CreatedDate)

(将表更改为表的实际名称。)

对于 SQLServer,执行以下操作:

SELECT table.ID, table.Name, table.CreatedDate
FROM table JOIN
  (SELECT MAX(CreatedDate) CreatedDate, Name FROM table GROUP BY Name) max_date
ON table.Name = max_date.name AND table.CreatedDate = max_date.CreatedDate
SELECT ID, Name, CreatedDate
FROM table JOIN
  (SELECT MAX(CreatedDate) CreatedDate, Name FROM table GROUP BY Name) max_date
USING (Name, CreatedDate)

(Change table to the actual name of the table.)

For SQLServer do:

SELECT table.ID, table.Name, table.CreatedDate
FROM table JOIN
  (SELECT MAX(CreatedDate) CreatedDate, Name FROM table GROUP BY Name) max_date
ON table.Name = max_date.name AND table.CreatedDate = max_date.CreatedDate
停顿的约定 2024-12-02 12:54:27

试试这个

SELECT myTable.Id, myTable.Name, myTable.CreatedDate
FROM table as myTable
INNER JOIN (SELECT Name, MAX(CreatedDate) AS MaxDate FROM table GROUP BY Name) 
    AS MaxRecord ON MaxRecord.Name = myTable.Name 
                AND MaxRecord.MaxDate= myTable.CreatedDate

Try this out

SELECT myTable.Id, myTable.Name, myTable.CreatedDate
FROM table as myTable
INNER JOIN (SELECT Name, MAX(CreatedDate) AS MaxDate FROM table GROUP BY Name) 
    AS MaxRecord ON MaxRecord.Name = myTable.Name 
                AND MaxRecord.MaxDate= myTable.CreatedDate
私藏温柔 2024-12-02 12:54:27

这将在 SQL Server 上产生您想要的结果:

select distinct x.ID, x.Name, x.CreatedDate
from table t
    cross apply (
        select top 1 ID, Name, CreatedDate
        from table t1 
        where t1.Name = t.Name
        order by CreatedDate desc
    ) x
order by x.Name;

This will produce the result you want on SQL Server:

select distinct x.ID, x.Name, x.CreatedDate
from table t
    cross apply (
        select top 1 ID, Name, CreatedDate
        from table t1 
        where t1.Name = t.Name
        order by CreatedDate desc
    ) x
order by x.Name;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文