更新带有整数序列的Postgres列,以供唯一对值

发布于 2025-01-23 07:54:32 字数 2388 浏览 0 评论 0原文

我有postgres表user_team具有列user_pkteam,练习,nistion> nistioned_at我想自动填充order列,其中包括user_pk and team 的唯一对,并由<代码>分配了: 因此,我拥有的列是:

|  user_pk    |  team          |  exercise_pk  | assigned_at  |  
|-------------------------------------------------------------|
| 111         | blue           |  "exercise"   | 2022-03-01   |
| 111         | blue           |  "exercise"   | 2022-03-02   |
| 222         | blue           |  "exercise"   | 2022-03-01   |
| 222         | blue           |  "exercise"   | 2022-03-02   |
| 222         | blue           |  "exercise"   | 2022-03-03   |
| 111         | green          |  "exercise"   | 2022-03-01   |
| 111         | green          |  "exercise"   | 2022-03-02   |
| 111         | green          |  "exercise"   | 2022-03-03   |
| 333         | green          |  "exercise"   | 2022-03-01   |
| 333         | green          |  "exercise"   | 2022-03-02   |

我想

|  user_pk    |  team          |  exercise_pk  | assigned_at  | order|
|--------------------------------------------------------------------|
| 111         | blue           |  "exercise"   | 2022-03-01   |1     |
| 111         | blue           |  "exercise"   | 2022-03-02   |2     |
| 222         | blue           |  "exercise"   | 2022-03-01   |1     |
| 222         | blue           |  "exercise"   | 2022-03-02   |2     |
| 222         | blue           |  "exercise"   | 2022-03-03   |3     |
| 111         | green          |  "exercise"   | 2022-03-01   |1     |
| 111         | green          |  "exercise"   | 2022-03-02   |2     |
| 111         | green          |  "exercise"   | 2022-03-03   |3     |
| 333         | green          |  "exercise"   | 2022-03-01   |1     |
| 333         | green          |  "exercise"   | 2022-03-02   |2     |

在一个查询中有任何方法可以做到这一点吗? 我尝试了不同的user_pk,team和答案来自:用整数序列更新Postgres列

    update bar b
   set id = b2.new_id
   from (select b.*, row_number() over (order by id) as new_id
         from bar
        ) b2;
   where b.pk = b2.pk;

但仍然无法弄清楚它

I have the postgres table user_team which has columns user_pk, team, exercise, assigned_at and I want to auto-populate the order column with sequence of incrementing integers but only for unique pairs of user_pk and team and ordered by assigned_at:
so the column which I have is:

|  user_pk    |  team          |  exercise_pk  | assigned_at  |  
|-------------------------------------------------------------|
| 111         | blue           |  "exercise"   | 2022-03-01   |
| 111         | blue           |  "exercise"   | 2022-03-02   |
| 222         | blue           |  "exercise"   | 2022-03-01   |
| 222         | blue           |  "exercise"   | 2022-03-02   |
| 222         | blue           |  "exercise"   | 2022-03-03   |
| 111         | green          |  "exercise"   | 2022-03-01   |
| 111         | green          |  "exercise"   | 2022-03-02   |
| 111         | green          |  "exercise"   | 2022-03-03   |
| 333         | green          |  "exercise"   | 2022-03-01   |
| 333         | green          |  "exercise"   | 2022-03-02   |

and I want to have

|  user_pk    |  team          |  exercise_pk  | assigned_at  | order|
|--------------------------------------------------------------------|
| 111         | blue           |  "exercise"   | 2022-03-01   |1     |
| 111         | blue           |  "exercise"   | 2022-03-02   |2     |
| 222         | blue           |  "exercise"   | 2022-03-01   |1     |
| 222         | blue           |  "exercise"   | 2022-03-02   |2     |
| 222         | blue           |  "exercise"   | 2022-03-03   |3     |
| 111         | green          |  "exercise"   | 2022-03-01   |1     |
| 111         | green          |  "exercise"   | 2022-03-02   |2     |
| 111         | green          |  "exercise"   | 2022-03-03   |3     |
| 333         | green          |  "exercise"   | 2022-03-01   |1     |
| 333         | green          |  "exercise"   | 2022-03-02   |2     |

Is there any way to do that in one query?
I tried with DISTINCT user_pk, team and with answer from: Updating postgres column with sequence of integers :

    update bar b
   set id = b2.new_id
   from (select b.*, row_number() over (order by id) as new_id
         from bar
        ) b2;
   where b.pk = b2.pk;

But still cannot figure it out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

趁年轻赶紧闹 2025-01-30 07:54:32

您寻找一个类似的问题是很好的。但是,您实际上是在您的问题上尝试了什么,您不能仅从那里复制答案,并期望它甚至与您需要的内容相关。这只是提供了可能适用的格式。

您需要的是识别唯一的列或一组唯一的列来识别特定行,从那里使用row_number为每个唯一值/集合分配所需的值,最终匹配生成/提取的标识符以获取所需的结果列。在下面的CTE使用列user_pk,Team,AndissEd_at以识别特定行和目标的新列值。然后,主要部分与这些列匹配以进行更新。 (请参阅

with ord_num( user_pk, team, assigned_at, order_seq)  as 
     ( select user_pk, team, assigned_at
            , row_number() over (partition by  user_pk, team 
                                     order by  user_pk, team, assigned_at
                                )
         from user_team 
      )
update user_team ut
   set order_seq = 
       (select order_seq 
          from ord_num os 
         where (ut.user_pk, ut.team, ut.assigned_at) = 
               (os.user_pk, os.team, os.assigned_at)
       ) ;

d63adbc16c171f643b7520666666e24242424240 >用户order作为列名称是极为糟糕的实践。它既是Postgres又是SQL Standard nofollow noreferrer“>保留词。虽然您可以摆脱它,但最好的结果就是通过做不正确的事情而没有提供任何信息来混淆,最坏的处理失败,而处理得最糟糕的失败。导致完整的数据损坏。也适用于任何保留的单词。

It is good that you looked for a similar question. But what did you actually try on your issue, you cannot just copy an answer from there and expect it to even relate to what you need. That just provides a format that may be applicable.

What you need is to identify a unique column or a unique set of columns to identify the specific rows, from there use row_number to assign the desired value for each unique value/set, finally match the generated/extracted identifier to get the desired result column. In the below the CTE used columns user_pk, team, assigned_at to identify specific rows and the targeted new column value. The main part then matched those column for update. (see demo)

with ord_num( user_pk, team, assigned_at, order_seq)  as 
     ( select user_pk, team, assigned_at
            , row_number() over (partition by  user_pk, team 
                                     order by  user_pk, team, assigned_at
                                )
         from user_team 
      )
update user_team ut
   set order_seq = 
       (select order_seq 
          from ord_num os 
         where (ut.user_pk, ut.team, ut.assigned_at) = 
               (os.user_pk, os.team, os.assigned_at)
       ) ;

NOTE: It is extremely poor practice to user order as a column name. It is both a Postgres and SQL Standard reserved word. While you can get away with it, the best outcome is just confusion, at worst processed fail, by doing the incorrect thing and not providing any message. Resulting in complete data corruption. Also applies to any reserved word.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文