Sql server 基于重复的组值获取数据

Sql server 基于重复的组值获取数据,sql-server,window-functions,sql-server-2017,Sql Server,Window Functions,Sql Server 2017,我不知道如何正确命名这个问题,但以下是示例数据: CREATE TABLE dbo.test_data ( row_version VARBINARY(8) , account_number CHAR(8) , account_balance DECIMAL(10, 2) , group_rank BIGINT , rownum BIGINT ); INSERT INTO dbo.test_data VALUES (0x0000

我不知道如何正确命名这个问题,但以下是示例数据:



CREATE TABLE dbo.test_data
(
    row_version     VARBINARY(8)
  , account_number  CHAR(8)
  , account_balance DECIMAL(10, 2)
  , group_rank      BIGINT
  , rownum          BIGINT
);

INSERT INTO dbo.test_data
VALUES (0x000000000013fd24, '46436663', 123.00, 4, 86)
     , (0x000000000013fd23, '46436663', 123.00, 4, 86)
     , (0x000000000013fd22, '46436663', 123.00, 4, 85)
     , (0x000000000013fd21, '46436663', 123.00, 4, 85)
     , (0x000000000013fd20, '46436663', 123.00, 4, 83)
     , (0x000000000013fd1f, '46436663', 555.00, 2, 83)
     , (0x000000000013fd21, '46436663', 123.00, 4, 85)
     , (0x000000000013fd20, '46436663', 123.00, 4, 83)
     , (0x000000000013fd21, '46436663', 123.00, 4, 85)
     , (0x000000000013fd20, '46436663', 123.00, 4, 83)
     , (0x000000000013fd1e, '46436664', 12345.00, 5, 82)
     , (0x000000000013fd1d, '46436664', 12345.00, 5, 82)
     , (0x000000000013fd1c, '46436664', 12345.00, 5, 82)
     , (0x000000000013fd1b, '46436664', 12345.00, 5, 81)
     , (0x000000000013fd1a, '46436664', 12345.00, 5, 81)
     , (0x000000000013fd19, '46436664', 12345.00, 5, 78)
     , (0x000000000013fcb3, '46436664', 123.00, 6, 77)
     , (0x000000000013fcb2, '46436664', 123.00, 6, 77)
     , (0x000000000013fcb1, '46436664', 123.00, 6, 76)
     , (0x000000000013fcb0, '46436664', 123.00, 6, 76);
以下是数据的外观:

SELECT * FROM dbo.test_data
ORDER BY row_version DESC
这里1和4蓝色是顺序组号,最小顺序为2。在一卷中,相同的值按行顺序排列。我需要找到第一个出现的不同组的秩2,红色,然后检查rownum值3,6,紫色,上面组的蓝色是MINrow_num,在组结束红色之前的记录是row_num。如果这些值相差1,那么我需要输入帐号,然后我需要返回它,否则-我不需要返回它

我对账户低于2分和5分的情况不感兴趣


因此,通过查看该数据,应该返回唯一的帐户-46436664,因为46436663的rownum值与83相同。

有趣的问题我想第一步是找到问题帐户号,您可以这样做

select 
    * 
from 
    dbo.test_data t1
cross apply (
    select top 1 
        * 
    from 
        dbo.test_data 
    where 
        account_number = t1.account_number
        and rownum = t1.rownum
        and row_version > t1.row_version 
    order by 
        row_version asc) t2
where 
    t1.group_rank <> t2.group_rank 
order by
    t1.row_version;
那你就可以这么做了

select distinct 
    t0.account_number 
from 
    test_data t0

except

select distinct
    t1.account_number 
from 
    dbo.test_data t1
cross apply (
    select top 1 
        * 
    from 
        dbo.test_data 
    where 
        account_number = t1.account_number
        and rownum = t1.rownum
        and row_version > t1.row_version 
    order by 
        row_version asc) t2
where 
    t1.group_rank <> t2.group_rank 

有趣的问题我想第一步是找到问题账号,你可以这样做

select 
    * 
from 
    dbo.test_data t1
cross apply (
    select top 1 
        * 
    from 
        dbo.test_data 
    where 
        account_number = t1.account_number
        and rownum = t1.rownum
        and row_version > t1.row_version 
    order by 
        row_version asc) t2
where 
    t1.group_rank <> t2.group_rank 
order by
    t1.row_version;
那你就可以这么做了

select distinct 
    t0.account_number 
from 
    test_data t0

except

select distinct
    t1.account_number 
from 
    dbo.test_data t1
cross apply (
    select top 1 
        * 
    from 
        dbo.test_data 
    where 
        account_number = t1.account_number
        and rownum = t1.rownum
        and row_version > t1.row_version 
    order by 
        row_version asc) t2
where 
    t1.group_rank <> t2.group_rank 

那么,你到底想要什么结果?我不适合你。如果你说的是预期结果,那么它是46436664,那么你的预期结果就是46436664?没别的了?是的,只是单身那么,你到底想要什么结果?我不适合你。如果你说的是预期结果,那么它是46436664,那么你的预期结果就是46436664?没有别的了?是的,只是单身