带有row_counter值的更新表
我觉得这应该很容易,但是我度过了最困难的时光。我正在使用SQL Server。 I'm trying to get the output from the following query to be used in an update statement to add to my table:
SELECT cons_id, credited_date,
ROW_NUMBER() OVER (partition by cons_id order by cons_id) AS Row_Counter
FROM fy23_jul_aug_anniv_jv;
My primary key is cons_id. The output looks like this:
I want the row_counter column to be a permanent column在我的表中称为计数器(我已经添加到表中...我只需要更新它)。我已经尝试过,但是我有一个错误说
sql命令未正确结束。
UPDATE fy23_jul_aug_anniv_jv
SET counter = b.row_counter
FROM (SELECT cons_id, ROW_NUMBER() OVER (partition by cons_id order by cons_id) AS Row_Counter
FROM fy23_jul_aug_anniv_jv) AS b
WHERE b.cons_id = cons_id;
I feel like this should be easy but I'm having the hardest time. I'm using SQL server. I'm trying to get the output from the following query to be used in an update statement to add to my table:
SELECT cons_id, credited_date,
ROW_NUMBER() OVER (partition by cons_id order by cons_id) AS Row_Counter
FROM fy23_jul_aug_anniv_jv;
My primary key is cons_id. The output looks like this:
I want the row_counter column to be a permanent column in my table called COUNTER (which I already added to my table...I just need to update it). I've tried this but I get an error saying
SQL command not properly ended.
UPDATE fy23_jul_aug_anniv_jv
SET counter = b.row_counter
FROM (SELECT cons_id, ROW_NUMBER() OVER (partition by cons_id order by cons_id) AS Row_Counter
FROM fy23_jul_aug_anniv_jv) AS b
WHERE b.cons_id = cons_id;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用常见表表达式(CTE)
You can use the common table expression (CTE)