将 PostgreSQL table 列值拆分为多列

Split PostgreSQL table column values into multiple columns

我正在使用 PostgreSQL,我有一个包含 2 列 manager_idmanager_name 的 table。我想将 manager_name 列(值例如:ANGLO EASTERN SHIPMANAGEMENT - HONG KONG, CHINA)拆分为经理姓名|城市 |国家。分隔符 "-" 和 "," .

我试过这个查询,但它只显示结果,并没有永久保存那些列

select 
    manager_name,
    split_part(manager_name, '-', 1) m_name,
    split_part(manager_name, ',', 2) city,
    split_part(manager_name, ',', 2) country 
from 
    manager2

是否有任何其他方法可以拆分此列并将其永久保存在 table 中??

也许有点冗长,但下面似乎可以做到。您从 manager_name 检索城市的 SPLIT_PART() 需要一些调整;还将它们包装在 TRIM() 中以删除前导和尾随空格。

CREATE TABLE manager2
(
  manager_id SERIAL PRIMARY KEY,
  manager_name VARCHAR(250)
);

INSERT INTO manager2 (manager_name)
VALUES ('ANGLO EASTERN SHIPMANAGEMENT - HONG KONG, CHINA');
INSERT INTO manager2 (manager_name)
VALUES ('HARTMANN REEDEREI - LEER, GERMANY');

-- add columns to table
ALTER TABLE manager2 ADD COLUMN m_name VARCHAR(250);
ALTER TABLE manager2 ADD COLUMN city VARCHAR(250);
ALTER TABLE manager2 ADD COLUMN country VARCHAR(250);

-- populate tables with vals
WITH results AS (
  SELECT manager_id AS id, TRIM(SPLIT_PART(manager_name, '-', 1)) AS m_name, 
  TRIM(SPLIT_PART(SPLIT_PART(manager_name, '-', 2), ',', 1)) AS city,  
  TRIM(SPLIT_PART(manager_name, ',', 2)) AS country
  FROM manager2
  )
UPDATE manager2
SET 
m_name = (SELECT m_name FROM results WHERE manager_id = results.id),
city = (SELECT city FROM results WHERE manager_id = results.id),
country = (SELECT country FROM results WHERE manager_id = results.id);

Table 更新后:

 manager_id |                  manager_name                   |            m_name            |   city    | country
------------+-------------------------------------------------+------------------------------+-----------+---------
          1 | ANGLO EASTERN SHIPMANAGEMENT - HONG KONG, CHINA | ANGLO EASTERN SHIPMANAGEMENT | HONG KONG | CHINA
          2 | HARTMANN REEDEREI - LEER, GERMANY               | HARTMANN REEDEREI            | LEER      | GERMANY

更新以处理像 'PSA - SINGAPORE' 和 PSA | NULL | SINGAPORE 这样的情况。您可以使用以下代码代替上面使用的 CTE (WITH results AS (...)):

WITH results AS (
  SELECT manager_id AS id, TRIM(SPLIT_PART(manager_name, '-', 1)) AS m_name,
    CASE
      WHEN SPLIT_PART(manager_name, '-', 2) ~ ',' THEN
        TRIM(SPLIT_PART(SPLIT_PART(manager_name, '-', 2), ',', 1))
      ELSE
        NULL
    END AS city,
    CASE 
      WHEN SPLIT_PART(manager_name, '-', 2) ~ ',' THEN 
        TRIM(SPLIT_PART(SPLIT_PART(manager_name, '-', 2), ',', 2)) 
      ELSE
        TRIM(SPLIT_PART(SPLIT_PART(manager_name, '-', 2), ',', 1)) 
    END AS country
  FROM manager2
  )

结果:

 manager_id |                  manager_name                   |            m_name            |   city    |  country
------------+-------------------------------------------------+------------------------------+-----------+-----------
          1 | ANGLO EASTERN SHIPMANAGEMENT - HONG KONG, CHINA | ANGLO EASTERN SHIPMANAGEMENT | HONG KONG | CHINA
          2 | HARTMANN REEDEREI - LEER, GERMANY               | HARTMANN REEDEREI            | LEER      | GERMANY
          3 | PSA - SINGAPORE                                 | PSA                          |           | SINGAPORE