从数据库中检索每个组中的最后一条记录 - SQL Server 2005/2008 [英] Retrieving last record in each group from database - SQL Server 2005/2008

查看:31
本文介绍了从数据库中检索每个组中的最后一条记录 - SQL Server 2005/2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一些搜索,但似乎无法获得我正在寻找的结果.基本上,我们整个公司有四个不同的管理系统,我正在定期合并来自每个系统的所有数据.我的目标是每小时将数据更新到中央数据库中.这是我正在使用的示例数据集:

I have done some seaching by can't seem to get the results I am looking for. Basically we have four different management systems in place throughout our company and I am in the process of combining all the data from each system on a regular basis. My goal is to update the data every hour into a central database. Here is a sample data set I am working with:

COMPUTERNAME | SERIALNUMBER | USERNAME | LASTIP | LASTUPDATE | SOURCE
TEST1 | 1111 | BOB | 1.1.1.1 | 1/17/2011 01:00:00 | MGMT_SYSTEM_1
TEST1 | 1111 | BOB | 1.1.1.1 | 1/18/2011 01:00:00 | MGMT_SYSTEM_2
TEST1 | 1111 | PETER | 1.1.1.11 | 1/19/2011 01:00:00 | MGMT_SYSTEM_3
TEST2 | 2222 | GEORGE | 1.1.1.2 | 1/17/2011 01:00:00 | MGMT_SYSTEM_1
TEST3 | 3333 | TOM | 1.1.1.3 | 1/19/2011 01:00:00 | MGMT_SYSTEM_2
TEST4 | 4444 | MIKE   | 1.1.1.4 | 1/17/2011 01:00:00 | MGMT_SYSTEM_1
TEST4 | 4444 | MIKE   | 1.1.1.41 | 1/19/2011 01:00:00 | MGMT_SYSTEM_3
TEST5 | 5555 | SUSIE  | 1.1.1.5 | 1/19/2011 01:00:00 | MGMT_SYSTEM_1

所以我想查询这个主表并且只检索最新的记录(基于 LASTUPDATE),这样我就可以获得关于该系统的最新信息.问题是每个数据库中可能有一个系统,但当然它们永远不会有完全相同的更新时间.

So I want to query this master table and only retrieve the latest record (based on LASTUPDATE) that way I can get the latest info about that system. The problem is that one system may be in each database, but of course they will never have the same exact update time.

我希望得到这样的东西:

I would expect to get something like this:

TEST1 | 1111 | PETER | 1.1.1.11 | 1/19/2011 01:00:00 | MGMT_SYSTEM_3
TEST2 | 2222 | GEORGE | 1.1.1.2 | 1/17/2011 01:00:00 | MGMT_SYSTEM_1
TEST3 | 3333 | TOM | 1.1.1.3 | 1/19/2011 01:00:00 | MGMT_SYSTEM_2
TEST4 | 4444 | MIKE   | 1.1.1.41 | 1/19/2011 01:00:00 | MGMT_SYSTEM_3
TEST5 | 5555 | SUSIE  | 1.1.1.5 | 1/19/2011 01:00:00 | MGMT_SYSTEM_1

我尝试过使用 MAX 函数,但是我只能检索一列.我不能在子查询中使用它,因为我没有一个唯一的 ID 字段可以给我最后更新的记录.其中一个系统是 MySQL 数据库,MySQL 中的 MAX 函数实际上将按照我需要的方式工作,每个 GROUP BY 只返回一条记录,但它在 SQL Server 中不起作用.

I have tried using the MAX function, but with that I can only retrieve one column. And I can't use that in a subquery because I don't have a unique ID field that would give me the last updated record. One of the systems is a MySQL database and the MAX function in MySQL will actually work the way I need it to only returning one record per GROUP BY, but it doesn't work in SQL Server.

我想我需要使用 MAX 和 LEFT JOIN,但到目前为止我的尝试都失败了.

I'm thinking I need to use MAX and a LEFT JOIN, but my attempts so far have failed.

非常感谢您的帮助.在过去的 3-4 个小时里,我一直在绞尽脑汁试图获得一个有效的查询.此主表位于 SQL Server 2005 服务器上.

Your help would be greatly appreciated. I have been racking my brain for the past 3-4 hours trying to get a working query. This master table is located on a SQL Server 2005 server.

谢谢!

推荐答案

;with cteRowNumber as (
    select COMPUTERNAME, SERIALNUMBER, USERNAME, LASTIP, LASTUPDATE, SOURCE,
           row_number() over(partition by COMPUTERNAME order by LASTUPDATE desc) as RowNum
        from YourTable
)
select COMPUTERNAME, SERIALNUMBER, USERNAME, LASTIP, LASTUPDATE, SOURCE
    from cteRowNumber
    where RowNum = 1

这篇关于从数据库中检索每个组中的最后一条记录 - SQL Server 2005/2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆