Sql 行号编号任意值重新启动以更改值
我有一个表xxx,其结构和列如下() 具有独特的约束Sql 行号编号任意值重新启动以更改值,sql,sql-server,rank,gaps-and-islands,row-number,Sql,Sql Server,Rank,Gaps And Islands,Row Number,我有一个表xxx,其结构和列如下() 具有独特的约束 ALTER TABLE [dbo].[xxx] ADD UNIQUE NONCLUSTERED ( [GameNo] ASC,[GameID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEM
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列有常量位值(对于连续的10条记录,GameAbout状态应为0),则应选择该用户标识
样本记录:
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
例如,userid20的GameWonLose列的连续位值为1,其中
该列为0。因此,如果gamewonloose列的位值更改为0,然后再次更改为1,这不是
考虑过的。 在这里,我需要找到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位列更改,但当值更改为
(1 0 1 10 0 0 0)类似于(1 1 2 3 2 3 4)。看看GameWonLose=1(赢),那么同一行中如何记录GameAbout=1(意味着游戏取消)。 是否可能?我是否遵循了?如果只有一列名为GameStatus(tinyint) 0-lose,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场比赛,放弃1场比赛,然后再次连续赢了10场比赛而不放弃一样,最后10场胜利将选择该用户ID
;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)