当我检查排名的频率低于排名变化的频率时,如何检测排名变化?
我有一些像这样的原始数据:
团队 | 排名 |
---|---|
1 | 1 |
2 | 8 |
3 | 2 |
4 6 | 5 |
3 | 6 |
5 | 7 |
4 | 8 |
7 | 每月 |
一次,我将其复制到我的数据库中,添加“上次更新”列以指示每月更新运行该行的最后更新时间。然而,由于排名的工作方式,如果一个排名发生变化,那么其他一些排名也会发生变化。 我的问题是,当相关行的排名发生具体更改时,我只想更新“上次更新”列。例如,如果第 5 队上升了两个排名,那么我只想将 Team 5 的行标记为已更新。我不想更改他们刚刚超越的两支球队的行的“最后更新”列中的任何内容。我怎样才能实现这个?
一个关键因素是有意的更改一次只能发生一个,但由于我只每月检查一次,所以我几乎总是会看到多个更改。困难在于检测哪支球队的排名是有意移动的,而不是仅仅由于其他数据而移动。
例如,考虑一下我每月检查之间的原始数据是否发生了以下两个变化(粗体是有意更改):
团队 | 原始排名 | 第一次更改后的排名 | 第二次更改后的排名 |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 8 | 8 | 8 |
3 | 2 | 5 6 | 4 |
6 | 6 | 7 | 5 |
3 | 2 | | 2 |
6 | 5 | 4 | 4 |
7 | 4 | 3 | 3 |
8 | 7 | 7 | 5 |
如果发生这种情况,那么我希望处理的数据中每个更改的行都更新其排名,但我只想更新团队 3 和团队 8 的“上次更新”列。
例如,如果现在是第 36 个月,我希望我处理的数据来自
团队 | 排名 | 上次更新 |
---|---|---|
1 | 1 | 31 |
2 | 8 | 31 |
3 | 2 | 32 |
4 | 6 | 33 |
5 | 3 | 34 |
6 | 5 | 35 |
7 | 4 | 31 |
8 | 7 | 31 |
至
团队 | 排名 | 最后更新 |
---|---|---|
1 | 1 | 31 |
2 | 8 | 31 |
3 | 6 | |
36 4 | 7 | 33 |
5 | 2 | 34 |
6 | 4 | 35 |
7 | 3 | 31 |
8 | 5 | 36 |
这可以在 T-SQL 中实现吗?此时,即使我必须求助于使用游标,我也不在乎。
I have some raw data like this:
Team | Ranking |
---|---|
1 | 1 |
2 | 8 |
3 | 2 |
4 | 6 |
5 | 3 |
6 | 5 |
7 | 4 |
8 | 7 |
Once a month, I copy it in to my database, adding a "Last Updated" column to indicate which monthly update run that row was last updated on. However, due to the way that ranks work, if one rank changes, then some other(s) will change. My issue is that I want to only update the "Last Updated" column when the rank of the row in question has been specifically changed. For example, if Team 5 moves up two ranks, then I only want to mark Team 5's row as updated. I do not want to change anything in the "Last Updated" column of the rows of the two teams that they have just overtaken. How can I implement this?
A key factor is that intentional changes can only happen one at a time, but since I'm only checking monthly, I'm almost always going to see multiple changes. The difficulty is in detecting which team's ranks have been intentionally moved rather than merely moved as a consequence of the other data.
For example, consider if the following two changes happened in the raw data between my monthly checks (intentional changes in bold):
Team | Original Ranking | Ranking After First Change | Ranking After Second Change |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 8 | 8 | 8 |
3 | 2 | 5 | 6 |
4 | 6 | 6 | 7 |
5 | 3 | 2 | 2 |
6 | 5 | 4 | 4 |
7 | 4 | 3 | 3 |
8 | 7 | 7 | 5 |
If that happens, then I want every changed row in my processed data to have its rank updated, but I only want the "Last Updated" column updated for Team 3 and Team 8.
For example, if it's now month 36, I'd want my processed data to go from
Team | Ranking | Last Updated |
---|---|---|
1 | 1 | 31 |
2 | 8 | 31 |
3 | 2 | 32 |
4 | 6 | 33 |
5 | 3 | 34 |
6 | 5 | 35 |
7 | 4 | 31 |
8 | 7 | 31 |
to
Team | Ranking | Last Updated |
---|---|---|
1 | 1 | 31 |
2 | 8 | 31 |
3 | 6 | 36 |
4 | 7 | 33 |
5 | 2 | 34 |
6 | 4 | 35 |
7 | 3 | 31 |
8 | 5 | 36 |
Is this possible to implement in T-SQL? At this point, I don't care even if I have to resort to using cursors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论语言如何,在算法级别上确定多个步骤都是不可能的。
示例:
初始团队排名:A、B、C
最终团队排名:B、C、A
您无法知道这是 A 从第 1 排名升至第 3 的一步 A,还是从第 1 排名升至第 2 的两步 A,然后从第二到第三。
Determining multiple steps is impossible on algorithmic level, regardless of language.
Example:
Initial team ranking: A,B,C
Final team ranking: B,C,A
You cannot know if this was a one step A moving from 1st rank to 3rd, or a two step of A going from 1st to 2nd and then from 2nd to 3rd.