使用组的最大值更新列
在 SQL Server 中,让我们考虑这个表:
Id | PartNumber | Timestamp | Reintegration |
---|---|---|---|
1 | 1111 | 2020-01-01 | NULL |
2 | 1111 | 2020-01-02 | NULL |
3 | 2222 | 2021-02-01 | NULL |
4 | 2222 | 2021-02-02 | NULL |
我想更新Reintegration
列Group By PartNumber
的 MAX
值。
这是预期的结果:
Id | PartNumber | Timestamp | Reintegration |
---|---|---|---|
1 | 1111 | 2020-01-01 | NULL |
2 | 1111 | 2020-01-02 | 2020-01-02 |
3 | 2222 | 2021-02-01 | NULL |
4 | 2222 | 2021-02-02 | 2021-02-02 |
我有使用GROUP BY
、MAX
、INNER JOIN
尝试了很多东西,但没有成功。
感谢您的任何帮助。
In SQL Server, let's consider this table:
Id | PartNumber | Timestamp | Reintegration |
---|---|---|---|
1 | 1111 | 2020-01-01 | NULL |
2 | 1111 | 2020-01-02 | NULL |
3 | 2222 | 2021-02-01 | NULL |
4 | 2222 | 2021-02-02 | NULL |
I want to update the Reintegration
column with the MAX
value of a Group By PartNumber
.
Here is the expected result :
Id | PartNumber | Timestamp | Reintegration |
---|---|---|---|
1 | 1111 | 2020-01-01 | NULL |
2 | 1111 | 2020-01-02 | 2020-01-02 |
3 | 2222 | 2021-02-01 | NULL |
4 | 2222 | 2021-02-02 | 2021-02-02 |
I have tried a lot of thing with GROUP BY
, MAX
, INNER JOIN
, without success.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
窗口函数
max() over()
似乎很适合这里。示例或dbFiddle
更新后的表< /强>
The window function
max() over()
seems like a nice fit here.Example or dbFiddle
The Updated Table