gpt4 book ai didi

PostgreSQL - 如何根据 PSQL View 中的公共(public)唯一标识符将 NULL 值替换为来自另一列的值

转载 作者:行者123 更新时间:2023-11-29 11:36:07 24 4
gpt4 key购买 nike

我的 PSQL View 中有三个外部标识符。我如何根据通用的 first_id 将 NULL second_id 值替换为 third_id 值?

目前:


first_id | second_id | third_id
----------+-----------+----------
1 | | 11
1 | | 11
1 | | 11
1 | 22 | 22
2 | 33 | 33
3 | 44 | 44
4 | 55 | 55
5 | 66 | 66
6 | | 77
6 | | 77
6 | | 77
6 | | 77
6 | 88 | 88

Should be:


first_id | second_id | third_id
----------+-----------+----------
1 | 22 | 11
1 | 22 | 11
1 | 22 | 11
1 | 22 | 22
2 | 33 | 33
3 | 44 | 44
4 | 55 | 55
5 | 66 | 66
6 | 88 | 77
6 | 88 | 77
6 | 88 | 77
6 | 88 | 77
6 | 88 | 88

How can I make this change?

  • The NULL values in the second_id column should be filled i.e. there shouldn't be blank cells.
  • If the second_id column shares a value with the third_id column, this value should fill the blank cells in the second_id column.
  • They should both be based on their common first_id.
  • Thanks so much. I really appreciate it.


    The second_id is really a CASE WHEN modification of the third_id. This modification is made in the view.

    VIEW:

        View "public.my_view"
    Column | Type | Modifiers | Storage | Description
    -----------------------------+-----------------------------+-----------+----------+-------------
    row_number | bigint | | plain |
    first_id | integer | | plain |
    second_id | integer | | plain |
    third_id | integer | | plain |
    first_type | character varying(255) | | extended |
    date_1 | timestamp without time zone | | plain |
    date_2 | timestamp without time zone | | plain |
    date_3 | timestamp without time zone | | plain |
    View definition:
    SELECT row_number() OVER (PARTITION BY t.first_id) AS row_number,
    t.first_id,
    CASE
    WHEN t.localization_key::text = 'rq.bkd'::text THEN t.third_id
    ELSE NULL::integer
    END AS second_id,
    t.third_id,
    t.first_type,
    CASE
    WHEN t.localization_key::text = 'rq.bkd'::text THEN t.created_at
    ELSE NULL::timestamp without time zone
    END AS date_1,
    CASE
    WHEN t.localization_key::text = 'st.appt'::text THEN t.created_at
    ELSE NULL::timestamp without time zone
    END AS date_2,
    CASE
    WHEN t.localization_key::text = 'st.eta'::text THEN t.created_at
    ELSE NULL::timestamp without time zone
    END AS date_3
    FROM my_table t
    WHERE (t.localization_key::text = 'rq.bkd'::text OR t.localization_key::text = 'st.appt'::text OR t.localization_key::text = 'st.eta'::text) AND t.first_type::text = 'thing'::text
    ORDER BY t.created_at DESC;

    这是指向 View 正在使用的表定义 (my_table) 的链接。

    https://gist.github.com/dankreiger/376f6545a0acff19536d

    再次感谢您的帮助。

    最佳答案

    您可以通过以下方式获取:

    select a.first_id, coalesce(a.second_id,b.second_id), a.third_id 
    from my_table a
    left outer join
    (
    select first_id, second_id from my_table
    where second_id is not null
    ) b
    using (first_id)

    所以更新应该是:

    update my_table a set second_id = b.second_id
    from
    (
    select first_id, second_id from my_table
    where second_id is not null
    ) b
    where b.first_id = a.first_id and a.second_id is null

    关于PostgreSQL - 如何根据 PSQL View 中的公共(public)唯一标识符将 NULL 值替换为来自另一列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30006384/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com