代码之家  ›  专栏  ›  技术社区  ›  Westerlund.io

基于具有共享数据的某些列创建标识符

  •  0
  • Westerlund.io  · 技术社区  · 6 年前

    在下面的示例中,列 sequence- 应该是在某些列中共享相同值的行的标识符- compania,hrentr,hrsaida,durjornada,durinterv,iniinterv,terminterv,sistema ou horrario,turno- -但是它不应该将列中的一个纳入计算中- dia-

    因此,如图中所述,前五行共享这些列,因此 sequence 应为1。第6行,它不共享所有以前的值,应该有自己的值 sequence number set to 2.

    我已经处理过 row_number()over(partition by… but it creates the opposite result i.e.it starts over when the matching columns stop.

    有没有办法创造出我想要的结果?-但是,它不应考虑计算其中一列- dia .

    如图所示,前五行共享这些列,因此 序列 应该是1。第6行,不共享所有以前的值,应该有自己的值 序列 数字设置为2。

    enter image description here

    我和他一起工作过 ROW_NUMBER() OVER (PARTITION BY... 但它会产生相反的结果,即当匹配列停止时,它会重新开始。

    有没有办法创造出我想要的结果?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ronen Ariely    6 年前

    可以使用rank()函数。检查是否可以解决您的需求:

    drop table if exists stackoverflowTbl;
    /********************************************************** DDL+DML */
    create table stackoverflowTbl(id int identity (1,1), txt int)
    GO
    insert stackoverflowTbl (txt) values (1),(1),(2),(1),(3),(22),(22)
    GO
    select * from stackoverflowTbl
    GO
    /********************************************************** solution */
    select id,txt,
        ROW_Number () OVER (order by txt) - RANK ( ) OVER ( partition by txt order by id ) as MySequence 
    from stackoverflowTbl
    GO