row_number编号任意值重新启动以更改值

时间:2013-12-28 10:21:43

标签: sql sql-server rank gaps-and-islands row-number

我有一个表格xxx,其中包含以下结构和列(SQL Fiddle

CREATE TABLE [dbo].[xxx]
(
    [GameNo] [int] IDENTITY(1,1) NOT NULL,
    [GameID] [varchar](500) NULL,
    [UserID] [int] NULL,
    [SteamID] [nvarchar](50) NULL,
    [GameWonLoose] [bit] NULL,
    [GameAbandon] [bit] NULL,
    [GamePlayDateTime] [datetime] NULL
)

有一个独特的约束。

ALTER TABLE [dbo].[xxx] ADD UNIQUE NONCLUSTERED 
(
    [GameNo] ASC,[GameID] ASC )WITH (PAD_INDEX  = OFF, 
                                    STATISTICS_NORECOMPUTE  = OFF,  
                                    SORT_IN_TEMPDB = OFF, 
                                    IGNORE_DUP_KEY = OFF, 
                                    ONLINE = OFF, 
                                    ALLOW_ROW_LOCKS  = ON, 
                                    ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
                                    )

如果我有GameWonLoose列的常量位值(对于连续GameAbandon状态的10条记录应为0),我应该选择该Userid。

样本记录:

GameNo  GameID      UserID  SteamID             GameWonLoose    GameAbandon  GamePlayDateTime
---------------------------------------------------------------------------------------------------
1       Chennai1    20      steamID 1:165440    1               0           2013-12-25 4:41:25.300
2       Chennai2    20      steamID 1:165440    1               0           2013-12-25 14:41:25.310
3       Chennai3    20      steamID 1:165440    1               0           2013-12-25 14:41:25.317
7       Chennai4    20      steamID 1:165440    1               0           2013-12-25 14:50:17.617
8       Chennai5    20      steamID 1:165440    1               0           2013-12-25 14:50:17.623
9       Chennai6    20      steamID 1:165440    1               0           2013-12-25 14:50:17.633
10      chennai2    27      steamID 1:165222    1               0           2013-12-28 11:09:56.823
11      chennai2    27      steamID 1:165222    0               0           2013-12-28 11:12:17.043
12      chennai3    27      steamID 1:165222    1               0           2013-12-28 11:12:17.053
13      chennai4    27      steamID 1:165222    1               0           2013-12-28 11:12:17.063
14      chennai5    27      steamID 1:165222    1               1           2013-12-28 11:12:17.070
15      chennai6    27      steamID 1:165222    0               0           2013-12-28 11:12:17.080
16      chennai7    8       steamID 1:174502    1               0           2013-12-28 11:13:13.790
17      chennai8    8       steamID 1:174502    0               0           2013-12-28 11:13:13.797
18      chennai7    27      steamID 1:165222    1               0           2013-12-28 11:13:38.517
19      chennai8    27      steamID 1:165222    0               1           2013-12-28 11:13:38.523
20      chennai7    35      steamID 1:184002    1               0           2013-12-28 11:14:05.037
21      chennai8    35      steamID 1:184002    1               0           2013-12-28 11:14:05.047
22      chennai9    8       steamID 1:174502    1               0           2013-12-28 11:14:45.253
23      chennai10   8       steamID 1:174502    1               1           2013-12-28 11:14:45.263
24      chennai11   8       steamID 1:174502    0               0           2013-12-28 11:14:45.270
25      chennai9    35      steamID 1:184002    1               0           2013-12-28 11:15:04.740
26      chennai10   35      steamID 1:184002    1               1           2013-12-28 11:15:04.747
27      chennai11   35      steamID 1:184002    0               0           2013-12-28 11:15:04.757

例如,UserId 20的GameWonLose列的连续位值为1 gameAbandon列是0.所以我选择那个用户ID(5连续记录),如果
gamewonloose列的位值变为0并再次变为1,它不是
考虑。 在这里我需要找到userid,如果对于该userid的gameno asc命令中的任何5条记录都是如此。

with x as
(
select GameNo,GameID,UserID, SteamID, GameWonLoose, GameAbandon,    
ROW_NUMBER()over(Partition by UserID Order by GameNo) as 'RowNumber' 
from xxx
)
select x.GameNo,x.GameID,x.UserID,x.SteamID,
x.GameWonLoose,x.GameAbandon,x.RowNumber,   
ROW_NUMBER()over(partition by x.UserId order by x.RowNumber),
ROW_NUMBER()over(partition by x.UserId order by x.RowNumber) 'RowNumber3' 
from x;

我尝试使用row_number开始编号为其中值的行 GameWonLoose位列更改,但当值更改为
时,它从2开始 (1 0 1 10 0 0 0)喜欢(1 1 2 3 2 3 4)。

1 个答案:

答案 0 :(得分:1)

看看GameWonLoose = 1(赢了),然后如何进入同一行/记录GameAbandon = 1(意味着游戏取消)。 是否有可能?我是否遵循过?如果只有一个名为GameStatus的列(tinyint) 0输,1 =赢,2 =游戏abondon等,然后它可以更容易管理。

如果可以的话,试试这个,

;with cte as
    (select *,ROW_NUMBER()over(Partition by UserID order by gameno)rn from @xxx
    where GameWonLoose=1 and GameAbandon=0)


    select * from cte where userid in(select userid from cte group by userid having max(rn)>=5)

'谢谢你的回答'

对于特定的用户ID,如果他/她连续赢得10场比赛(gameno)我需要拿到那个用户ID,在该游戏中放弃不被认为只考虑输掉胜利,就像我赢了3场比赛并输掉1而放弃一个并且在不放弃的情况下连续赢得10场比赛,最后10位将选择该用户标识