硬 tsql 问题 - 有多少行值按顺序排列
假设我有一个表
date,personid
1/1/2001 1
1/2/2001 3
1/3/2001 2
1/4/2001 2
1/5/2001 5
1/6/2001 5
1/7/2001 6
,我将使用 personid 2 更新 1/2/2001 或 1/5/2001,但在更新之前,我必须确保它通过了一条规则,表明您不能拥有一个人连续三天。 我该如何在 mssql 存储过程中解决这个问题?
更新:它还需要解决这个布局以及我更新 1/5/2001 的地方
date,personid
1/1/2001 1
1/2/2001 3
1/3/2001 2
1/4/2001 2
1/5/2001 1
1/6/2001 2
1/7/2001 2
1/8/2001 5
1/9/2001 5
1/10/2001 6
lets say I have a table with
date,personid
1/1/2001 1
1/2/2001 3
1/3/2001 2
1/4/2001 2
1/5/2001 5
1/6/2001 5
1/7/2001 6
and I'm going to either update 1/2/2001 or 1/5/2001 with personid 2 but before I can update I have to make sure it passes a rule that says you can't have a person three days in a row.
how can i solve this in a mssql stored procedure?
update: It also need to solve this layout as well where I'd update 1/5/2001
date,personid
1/1/2001 1
1/2/2001 3
1/3/2001 2
1/4/2001 2
1/5/2001 1
1/6/2001 2
1/7/2001 2
1/8/2001 5
1/9/2001 5
1/10/2001 6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设
date
是唯一的,如果情况并非如此,请告诉我!I've assumed that
date
is unique let me know if that is not the case!还有第三种情况没有列出,它是间隔日期的情况。我将其包含在下面的解决方案中。
输出是
There is a third case not listed, it is the between date case. I included it in the solution below.
The output is
这是我的参数化解决方案:
@date
表示应更新personid
列的日期。@personid
是要存储的新值。@MaxInARow
是允许存储相同personid
的连续最大天数。Here's my parametrised solution:
@date
represents the date for which thepersonid
column should be updated.@personid
is the new value to be stored.@MaxInARow
is the maximum number of days in a row for which the samepersonid
is allowed to be stored.