更新带有整数序列的Postgres列,以供唯一对值
我有postgres表user_team
具有列user_pk
,team
,练习,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您寻找一个类似的问题是很好的。但是,您实际上是在您的问题上尝试了什么,您不能仅从那里复制答案,并期望它甚至与您需要的内容相关。这只是提供了可能适用的格式。
您需要的是识别唯一的列或一组唯一的列来识别特定行,从那里使用row_number为每个唯一值/集合分配所需的值,最终匹配生成/提取的标识符以获取所需的结果列。在下面的CTE使用列
user_pk,Team,AndissEd_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)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.