获取同一张表的数据
我的表有点像
Name |DateOfEvent|EventType
----------------------------------
Smith |10/1/2005 |New
Thomas |1/1/2002 |Updated
Johnson |6/1/2002 |New
Smith |7/1/2008 |Updated
Smith |7/1/2000 |New
我想返回行,其中事件为“New”,日期位于同名行之前,但 EventType 已更新为较晚的日期。
我的想法是迭代带有名称的每一行的表,但这似乎效率很低。有更好的方法吗?
I have table sort of like
Name |DateOfEvent|EventType
----------------------------------
Smith |10/1/2005 |New
Thomas |1/1/2002 |Updated
Johnson |6/1/2002 |New
Smith |7/1/2008 |Updated
Smith |7/1/2000 |New
I want to return rows where the event is say New and the date is before a row with the same name but the EventType is Updated with a later date.
My thought is to iterate over the table with each row with the name but that seems really inefficient. Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者与 join 相同:
Or the same with join:
使用:
使用 JOIN(内部或外部)的问题是,如果有多个记录标记为“已更新”且日期早于“新”记录,则会出现重复的
x参考文献。
Use:
The problem with using a JOIN (INNER or OUTER) is that if there are more than one records that are marked "updated" with a date before the "new" one -- there'll be duplicates of the
x
references.也许是这样的:
此查询使用
Name
进行JOIN
但随着表的增长,名称可能不会保持唯一。引入主键和父 ID 来跟踪历史记录可能会效果更好。Maybe something along the lines of:
This query uses
Name
to do theJOIN
but names might not stay unique as the table grows. Introducing a primary key and parent id-s to keep track of the history will probably work better.