如何根据日期字段选择不同的数据?
我有一个表,用于存储另一个表中对象的更改日志。以下是我的表格内容:
ObjID Color Date User
------- ------- ------------------------ --------
1 Red 2010-01-01 12:22:00.000 Joe
1 Blue 2010-01-02 15:22:00.000 Jill
1 Green 2010-01-03 16:22:00.000 Joe
1 White 2010-01-10 09:22:00.000 Mike
2 Red 2010-01-09 10:22:00.000 Mike
2 Blue 2010-01-12 09:22:00.000 Jill
2 Orange 2010-01-12 15:22:00.000 Joe
我想选择每个对象的最近日期,以及该记录日期的颜色和用户。
基本上,我想要这个结果集:
ObjID Color Date User
------- ------- ------------------------ --------
1 White 2010-01-10 09:22:00.000 Mike
2 Orange 2010-01-12 15:22:00.000 Joe
我无法理解我需要编写的 SQL 查询来获取此数据...
我正在通过 ODBC 从 iSeries DB2 数据库 (AS/400) 检索数据。
I have table that stores a log of changes to objects in another table. Here are my table contents:
ObjID Color Date User
------- ------- ------------------------ --------
1 Red 2010-01-01 12:22:00.000 Joe
1 Blue 2010-01-02 15:22:00.000 Jill
1 Green 2010-01-03 16:22:00.000 Joe
1 White 2010-01-10 09:22:00.000 Mike
2 Red 2010-01-09 10:22:00.000 Mike
2 Blue 2010-01-12 09:22:00.000 Jill
2 Orange 2010-01-12 15:22:00.000 Joe
I want to select the most recent date for each Object, as well as the Color and User on the date of that record.
Bascically, I want this result set:
ObjID Color Date User
------- ------- ------------------------ --------
1 White 2010-01-10 09:22:00.000 Mike
2 Orange 2010-01-12 15:22:00.000 Joe
I'm having trouble wrapping my head around the SQL query I need to write to get this data...
I am retrieving data via ODBC from an iSeries DB2 database (AS/400).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
嘿,我想您想要以下内容(其中 ColorTable 是您的表名称):
Hey there, I think you want the following (where ColorTable is your table name):
假设至少有 SQL Server 2005
或评论中链接的文章中的 SQL Server 2000 方法
Assuming at least SQL Server 2005
Or a SQL Server 2000 method from the article linked to in the comments
如果您有一个对象表,并且您的 ObjectHistory 表在 ObjID 和日期上有一个索引,那么这可能比迄今为止给出的其他查询执行得更好:
只有当您也从对象表中提取列时,性能改进可能才会出现,但是值得一试。
如果您想要所有对象,无论它们是否有历史记录条目,请切换到
OUTER APPLY
(当然使用O.ObjID
而不是H.ObjID
)代码>)。这个查询的巧妙之处在于
If you have an Objects table and your ObjectHistory table has an index on ObjID and date, then this could perform better than other queries given so far:
The performance improvement may only come if you're pulling columns from the Objects table, too, but it's worth a shot.
If you want all Objects regardless of whether they have a history entry, switch to
OUTER APPLY
(and of course useO.ObjID
instead ofH.ObjID
).The neat thing about this query is that
请参阅以下两个相关问题:
SQL/mysql - 选择不同/唯一但返回所有列?
并且:
如何使用SQL高效地确定行之间的变化
See these two related questions:
SQL/mysql - Select distinct/UNIQUE but return all columns?
And:
How to efficiently determine changes between rows using SQL
您可以找到每个对象的最新更改,如下所示:
然后,您可以通过将上面返回的集(实例化为已指定别名的内联视图)再次链接到同一个表来获取颜色和用户列:
You can find out, per object, its most recent change like this:
You can then get the color and user columns by linking the set returned above, instantiated as an inline view that has been given an alias, to the same table again:
就像上面的马丁·史密斯一样,
只需在分区上输入行号并选择最新的行之一
喜欢
like martin smiths above,
simply just do a row number over partition and pick one of the rows that is most recent
like