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

查看:114
本文介绍了从数据库中检索每个组中的最后一条记录-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天全站免登陆