(子)组内的Oracle Rank()

时间:2017-04-20 15:14:04

标签: sql oracle oracle12c rank

我试图在一个小组内获得正确的排名,并且结果出现问题。我需要在每次基于时间戳更改组时获得排名。

例如,使用此表:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //some condition

    DropDownList ddl = new DropDownList();
    //some ddl properties
    ddl.AutoPostBack = true;
    ddl.SelectedIndexChanged += new EventHandler(ddl_OnSelectedIndexChanged);
}

我目前得到的是

private void ddl_OnSelectedIndexChanged(Object sender, EventArgs e)
{
    string test = "reached on select Index changed";
}

所需的输出如下:

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

1 个答案:

答案 0 :(得分:1)

您可以使用行号方法的差异将连续相同的状态行分类为一个组。 (运行内部查询以查看如何分配组。)然后对这些组使用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