我试图在一个小组中获得正确的排名,并且在结果方面遇到问题。我需要根据时间戳记每次组更改的等级。

例如,使用此表:

create table syntrans (
    transid     number,
    launchtime  timestamp,
    status      varchar2(10)
);

insert into syntrans values ( 1, '19-APR-17 07.34.05.824875 PM','FAIL');
insert into syntrans values (  1, '19-APR-17 07.34.06.828753 PM','FAIL');
insert into syntrans values (  1, '19-APR-17 07.34.08.567579 PM','SUCCESS');
insert into syntrans values (  1, '19-APR-17 08.07.31.731745 PM','SUCCESS');
insert into syntrans values (  1, '19-APR-17 08.07.32.735582 PM','SUCCESS');
insert into syntrans values (  2, '19-APR-17 08.17.51.332804 PM','FAIL');
insert into syntrans values (  2, '19-APR-17 08.17.52.336530 PM','FAIL');
insert into syntrans values (  2, '19-APR-17 08.19.27.993327 PM','SUCCESS');
insert into syntrans values (  2, '19-APR-17 08.25.54.860077 PM','FAIL');
insert into syntrans values (  2, '19-APR-17 08.25.55.862830 PM','SUCCESS');

我目前得到的是
SELECT transid,
       launchtime,
       status,
       rank() over (partition by status order by launchtime) rnk
  FROM syntrans
 order by transid, launchtime, status;

   TRANSID LAUNCHTIME                     STATUS            RNK
---------- ------------------------------ ---------- ----------
         1 19-APR-17 07.34.05.824875 PM   FAIL                1
         1 19-APR-17 07.34.06.828753 PM   FAIL                2
         1 19-APR-17 07.34.08.567579 PM   SUCCESS             1
         1 19-APR-17 08.07.31.731745 PM   SUCCESS             2
         1 19-APR-17 08.07.32.735582 PM   SUCCESS             3
         2 19-APR-17 08.17.51.332804 PM   FAIL                3
         2 19-APR-17 08.17.52.336530 PM   FAIL                4
         2 19-APR-17 08.19.27.993327 PM   SUCCESS             4
         2 19-APR-17 08.25.54.860077 PM   FAIL                5
         2 19-APR-17 08.25.55.862830 PM   SUCCESS             5

所需的输出如下所示:
   TRANSID LAUNCHTIME                     STATUS            RNK
---------- ------------------------------ ---------- ----------
         1 19-APR-17 07.34.05.824875 PM   FAIL                1
         1 19-APR-17 07.34.06.828753 PM   FAIL                2
         1 19-APR-17 07.34.08.567579 PM   SUCCESS             1
         1 19-APR-17 08.07.31.731745 PM   SUCCESS             2
         1 19-APR-17 08.07.32.735582 PM   SUCCESS             3
         2 19-APR-17 08.17.51.332804 PM   FAIL                1
         2 19-APR-17 08.17.52.336530 PM   FAIL                2
         2 19-APR-17 08.19.27.993327 PM   SUCCESS             1
         2 19-APR-17 08.25.54.860077 PM   FAIL                1
         2 19-APR-17 08.25.55.862830 PM   SUCCESS             1

...每次状态值更改(按日期排序)时,“排名”就会重新开始。我知道我得到的输出是按总体状态组进行排名的,但是我无法找到功能的任何组合来获得所需的输出。

这几乎可以实现,但是效果不尽如人意:
SELECT transid, launchtime, status, rnk
  FROM (SELECT transid,
              status,
              launchtime,
              RANK() OVER (PARTITION BY transid, status ORDER BY launchtime) rnk
         FROM syntrans )
ORDER BY transid, launchtime;

最佳答案

您可以使用行数差异法将连续的相同状态行分类为一组。 (运行内部查询以查看如何分配组。)然后将row_number与这些组一起使用。

SELECT transid, launchtime, status
,ROW_NUMBER() over(PARTITION BY transid,grp ORDER BY launchtime) as rnk
FROM (SELECT  transid,
              status,
              launchtime,
              ROW_NUMBER() OVER (PARTITION BY transid ORDER BY launchtime)
              -ROW_NUMBER() OVER (PARTITION BY transid, status ORDER BY launchtime) as grp
         FROM syntrans) t

关于sql - (子)组内的Oracle Rank(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43523514/

10-09 05:41