Oracle 查询返回字段值序列更改的记录

发布于 2024-10-06 12:17:50 字数 906 浏览 5 评论 0原文

给定以下一组记录,我想返回第一次出现的“已更改”记录。

给定(相关字段):


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭白衣梦中忆 2024-10-13 12:17:50

尝试以下查询。基本上,如果前一个状态与当前状态不同,则需要获取 STATE Columndisplay 当前行的下一个值。

create table state_data(
  id number,
  state varchar2(20),
  ref   varchar2(1),
  date1  date);


insert into state_data values (1     ,'state_1','x',to_date('2010-12-01','YYYY-MM-DD'));
insert into state_data values (2     ,'state_1','x',to_date('2010-12-02','YYYY-MM-DD'));
insert into state_data values (3     ,'state_2','x',to_date('2010-12-02','YYYY-MM-DD'));
insert into state_data values (4     ,'state_2','x',to_date('2010-12-02','YYYY-MM-DD'));
insert into state_data values (5     ,'state_1','Y',to_date('2010-12-03','YYYY-MM-DD'));
insert into state_data values (6     ,'state_3','x',to_date('2010-12-03','YYYY-MM-DD'));
insert into state_data values (7     ,'state_4','x',to_date('2010-12-03','YYYY-MM-DD'));
insert into state_data values (8     ,'state_2','x',to_date('2010-12-03','YYYY-MM-DD'));
insert into state_data values (9     ,'state_1','x',to_date('2010-12-03','YYYY-MM-DD'));
insert into state_data values (10    ,'state_1','x',to_date('2010-12-04','YYYY-MM-DD'));
insert into state_data values (11    ,'state_2','x',to_date('2010-12-04','YYYY-MM-DD'));
commit;

——然后是查询。

select id, state, ref, date1 from (
select id, state, ref, date1,
       lag(state) over (order by id asc) prev_state
  from state_data
  )
 where nvl(prev_state,'NULL') != nvl(state,'NULL');


        ID STATE                R DATE1
---------- -------------------- - ---------
         1 state_1              x 01-DEC-10
         3 state_2              x 02-DEC-10
         5 state_1              Y 03-DEC-10
         6 state_3              x 03-DEC-10
         7 state_4              x 03-DEC-10
         8 state_2              x 03-DEC-10
         9 state_1              x 03-DEC-10
        11 state_2              x 04-DEC-10

上面的结果比输出中显示的行有更多行,
但看看你的数据,这似乎是正确的结果。

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.

create table state_data(
  id number,
  state varchar2(20),
  ref   varchar2(1),
  date1  date);


insert into state_data values (1     ,'state_1','x',to_date('2010-12-01','YYYY-MM-DD'));
insert into state_data values (2     ,'state_1','x',to_date('2010-12-02','YYYY-MM-DD'));
insert into state_data values (3     ,'state_2','x',to_date('2010-12-02','YYYY-MM-DD'));
insert into state_data values (4     ,'state_2','x',to_date('2010-12-02','YYYY-MM-DD'));
insert into state_data values (5     ,'state_1','Y',to_date('2010-12-03','YYYY-MM-DD'));
insert into state_data values (6     ,'state_3','x',to_date('2010-12-03','YYYY-MM-DD'));
insert into state_data values (7     ,'state_4','x',to_date('2010-12-03','YYYY-MM-DD'));
insert into state_data values (8     ,'state_2','x',to_date('2010-12-03','YYYY-MM-DD'));
insert into state_data values (9     ,'state_1','x',to_date('2010-12-03','YYYY-MM-DD'));
insert into state_data values (10    ,'state_1','x',to_date('2010-12-04','YYYY-MM-DD'));
insert into state_data values (11    ,'state_2','x',to_date('2010-12-04','YYYY-MM-DD'));
commit;

-- and then the Query.

select id, state, ref, date1 from (
select id, state, ref, date1,
       lag(state) over (order by id asc) prev_state
  from state_data
  )
 where nvl(prev_state,'NULL') != nvl(state,'NULL');


        ID STATE                R DATE1
---------- -------------------- - ---------
         1 state_1              x 01-DEC-10
         3 state_2              x 02-DEC-10
         5 state_1              Y 03-DEC-10
         6 state_3              x 03-DEC-10
         7 state_4              x 03-DEC-10
         8 state_2              x 03-DEC-10
         9 state_1              x 03-DEC-10
        11 state_2              x 04-DEC-10

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文