Oracle 查询返回字段值序列更改的记录
给定以下一组记录,我想返回第一次出现的“已更改”记录。
给定(相关字段):
ID STATE REF DATE
1 state_1 x 2010-12-01
2 state_1 x 2010-12-02
3 state_2 x 2010-12-02
4 state_2 x 2010-12-02
5 state_1 Y 2010-12-03
6 state_3 x 2010-12-03
7 state_4 x 2010-12-03
8 state_2 x 2010-12-03
9 state_1 x 2010-12-03
10 state_1 x 2010-12-04
11 state_2 x 2010-12-04
所需返回(对于对象引用 X):
ID STATE REF DATE
1 state_1 x 2010-12-01
3 state_2 x 2010-12-02
6 state_3 x 2010-12-03
7 state_4 x 2010-12-03
8 state_2 x 2010-12-03
9 state_1 x 2010-12-03
11 state_2 x 2010-12-04
进一步说明: 这是一个保存对象状态的审核表,我想跟踪某个对象状态发生更改的日期。
我正在玩聚合/分析,但开始想知道它们是否能让我到达任何地方。
Given the following set of records, I'd like to return the first occurrence of 'changed' record.
Given (relevant fields):
ID STATE REF DATE
1 state_1 x 2010-12-01
2 state_1 x 2010-12-02
3 state_2 x 2010-12-02
4 state_2 x 2010-12-02
5 state_1 Y 2010-12-03
6 state_3 x 2010-12-03
7 state_4 x 2010-12-03
8 state_2 x 2010-12-03
9 state_1 x 2010-12-03
10 state_1 x 2010-12-04
11 state_2 x 2010-12-04
Desired return (for object ref X):
ID STATE REF DATE
1 state_1 x 2010-12-01
3 state_2 x 2010-12-02
6 state_3 x 2010-12-03
7 state_4 x 2010-12-03
8 state_2 x 2010-12-03
9 state_1 x 2010-12-03
11 state_2 x 2010-12-04
Further explanation:
This is an auditing table that keeps state of objects, and I'd like to track the dates that a change occurred to the state of a certain object.
I'm playing with aggregates/analytic but starting to wonder if they would get me anywhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下查询。基本上,如果前一个状态与当前状态不同,则需要获取 STATE Columndisplay 当前行的下一个值。
——然后是查询。
上面的结果比输出中显示的行有更多行,
但看看你的数据,这似乎是正确的结果。
Try the following Query. Basically, you need to get the next value of STATE Columndisplay the current row if if the previous state is different from the current one.
-- and then the Query.
The above result has additional rows than what you showed in the output,
but looking at your data, it seems like this is the correct result.