-1

我有以下数据集

color_code   fav_color_code    color_code_name    fav_color_name 
1|2            5                blue|white           black
3|4            7|9              green|red           pink|yellow

我需要将 color_code 的第一个值连接到 color_code_name 的第一个值,将 color_code 的第二个值连接到 color_code_name 的第二个值等。

我的结果应该如下所示:

code                color
1                    blue
2                    white
5                     black
3                     green
4                      red
7                      pink
9                     yellow

我正在使用下面的代码,但它正在交叉连接,因为我没有 id 可以加入。有人可以帮我得到预期的结果吗?

 SELECT 
        t1.code AS code, 
        t2.color AS color, 
      FROM 
        (
          SELECT 
            c.value :: varchar AS code, 
            row_number() over(
              order by 
                code
            ) AS rownum 
          FROM 
            table, 
            lateral flatten (
              input => split(color_code, '|')
            ) c  
          UNION 
          SELECT 
            d.value :: varchar AS code, 
            row_number() OVER(
              ORDER BY 
                code
            ) AS rownum
            FROM 
            table, 
            lateral flatten (
              input => split(fav_color_code, '|')
            ) d 
        ) t1 
        JOIN (
          SELECT 
            f.value :: varchar AS color, 
            row_number() OVER(
              ORDER BY 
                color
            ) AS rownum 
          FROM 
            table, 
            lateral flatten (
              input => split(color_code_name, '|')
            ) f 
          UNION 
          SELECT 
            g.value :: varchar AS color, 
            row_number() OVER(
              ORDER BY 
                color
            ) AS rownum 
          FROM 
            table, 
            lateral flatten (
              input => split(fav_color_name, '|')
            ) g 
        ) t2 ON (t1.rownum = t2.rownum) 
      ORDER BY 
        t1.color
````````````````````````````````````````````````````````
Thanks in advance
4

1 回答 1

1

笔记

下面的答案属于从这篇文章中删除的其他问题。

已删除问题 将同一表中的代码和描述结合起来

我有一张t_employee这样的桌子

id|name|profession
101|ava,julia,ann|beautician,musician,doctor
102|john,alice|doctor,singer
103|peter,philip,diane,lucy|teacher,police,teacher,dancer

我想在表格中转换t_employee表格t_worker

id|name|profession
101|ava|beutician
101|julia|musician
101|ann|doctor
102|john|doctor
102|alice|singer
103|peter|teacher
103|philip|police
103|diane|teacher
103|lucy|dancer

回答

一种方法是explode临时表中的第一个数组,explode第二个临时表中的第二个数组,然后join是它们。

举个例子

+----------------+--------------------------+--------------------------------+--+
| t_employee.id  |     t_employee.name      |     t_employee.profession      |
+----------------+--------------------------+--------------------------------+--+
| 101            | ava,julia,ann            | beautician,musician,doctor     |
| 102            | john,alice               | doctor,singer                  |
| 103            | peter,philip,diane,lucy  | teacher,police,teacher,dancer  |
+----------------+--------------------------+--------------------------------+--+
CREATE TABLE t1 AS
SELECT id, tname,ROW_NUMBER() OVER() AS rownum
FROM t_employee
LATERAL VIEW EXPLODE(SPLIT(name, ',')) t1 AS tname;

CREATE TABLE t2 AS
SELECT id, tprof,ROW_NUMBER() OVER() AS rownum
FROM t_employee
LATERAL VIEW EXPLODE(SPLIT(profession, ',')) t1 AS tprof;

CREATE TABLE t_worker AS
SELECT t1.id AS id, t1.tname AS name, t2.tprof AS profession
FROM t1
JOIN t2 ON(t1.id = t2.id AND t1.rownum = t2.rownum)
ORDER BY id;
SELECT *
FROM t_worker;

预期产出

+------+---------+-------------+--+
|  id  |  name   | profession  |
+------+---------+-------------+--+
| 101  | ava     | beautician  |
| 101  | julia   | musician    |
| 101  | ann     | doctor      |
| 102  | john    | doctor      |
| 102  | alice   | singer      |
| 103  | peter   | teacher     |
| 103  | philip  | police      |
| 103  | diane   | teacher     |
| 103  | lucy    | dancer      |
+------+---------+-------------+--+
于 2020-05-01T15:01:52.607 回答