带有row_counter值的更新表

发布于 2025-02-13 23:30:29 字数 931 浏览 0 评论 0原文

我觉得这应该很容易,但是我度过了最困难的时光。我正在使用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:

enter image description here

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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2025-02-20 23:30:29

您可以使用常见表表达式(CTE)

WITH CTE AS (
    SELECT counter ,ROW_NUMBER() OVER (partition by cons_id order by cons_id) AS Row_Counter
    FROM fy23_jul_aug_anniv_jv
)
UPDATE CTE SET counter = Row_Counter

You can use the common table expression (CTE)

WITH CTE AS (
    SELECT counter ,ROW_NUMBER() OVER (partition by cons_id order by cons_id) AS Row_Counter
    FROM fy23_jul_aug_anniv_jv
)
UPDATE CTE SET counter = Row_Counter
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文