简体   繁体   English

使用string_to_array()从Oracle到PostgreSQL查询转换

[英]Oracle to PostgreSQL query conversion with string_to_array()

I have below query in Oracle: 我在Oracle中有以下查询:

SELECT to_number(a.v_VALUE), b.v_VALUE
       FROM TABLE(inv_fn_splitondelimiter('12;5;25;10',';')) a
       JOIN TABLE(inv_fn_splitondelimiter('10;20;;', ';')) b
         ON a.v_idx = b.v_idx

which give me result like: 这给我的结果是:

在此处输入图片说明

I want to convert the query to Postgres. 我想将查询转换为Postgres。 I have tried a query like: 我试过像这样的查询:

SELECT UNNEST(String_To_Array('10;20;',';'))

I have also tried: 我也尝试过:

SELECT a,b
       FROM (select  UNNEST(String_To_Array('12;5;25;10;2',';'))) a
       LEFT JOIN (select  UNNEST(String_To_Array('12;5;25;10',';'))) b
         ON a = b

But didn't get a correct result. 但是没有得到正确的结果。
I don't know how to write query that's fully equivalent to the Oracle version. 我不知道如何编写完全等同于Oracle版本的查询。 Anyone? 任何人?

Starting with Postgres 9.4 you can use unnest() with multiple arrays to unnest them in parallel: 从Postgres 9.4开始,可以对多个数组使用unnest()来并行取消嵌套它们:

SELECT *
FROM   unnest('{12,5,25,10,2}'::int[]
            , '{10,20}'       ::int[]) AS t(col1, col2);

That's all. 就这样。 NULL values are filled in automatically for missing elements to the right. NULL值将自动填充到右侧缺少的元素。

If parameters are provided as strings , convert with string_to_array() first. 如果参数以字符串形式提供,请首先使用string_to_array()转换。 Like: 喜欢:

SELECT *
FROM   unnest(string_to_array('12;5;25;10', ';')
            , string_to_array('10;20'     , ';')) AS t(col1, col2);

More details and an alternative solution for older versions: 更多详细信息和较旧版本的替代解决方案:

In the expression select a the a is not a column, but the name of the table alias. 在表达式select aa不是一列,但表的别名的名称。 Consequently that expressions selects a complete row-tuple (albeit with just a single column), not a single column. 因此,该表达式选择一个完整的行元组(尽管只有一个列),而不是单个列。

You need to define proper column aliases for the derived tables. 您需要为派生表定义适当的列别名。 It is also recommended to use set returning functions only in the from clause, not in the select list. 还建议仅在from子句中而不是在select列表中使用set返回函数。

If you are not on 9.4 you need to generate the "index" using a window function. 如果您不在9.4上,则需要使用窗口函数生成“索引”。 If you are on 9.4 then Erwin's answer is much better. 如果您使用的是9.4,那么Erwin的答案会更好。

SELECT a.v_value, b.v_value
FROM (
   select row_number() over () as idx,  -- generate an index for each element
          i as v_value
   from UNNEST(String_To_Array('12;5;25;10;2',';')) i
) as a 
  JOIN (
     select row_number() over() as idx, 
            i as v_value
     from UNNEST(String_To_Array('10;20;;',';')) i
  ) as b 
  ON a.idx = b.idx;

An alternative way in 9.4 would be to use the with ordinality option to generate the row index in case you do need the index value: 9.4中的另一种方法是使用with ordinality选项生成行索引,以防您确实需要索引值:

select a.v_value, b.v_value
from regexp_split_to_table('12;5;25;10;2',';') with ordinality as a(v_value, idx)
  left join regexp_split_to_table('10;20;;',';') with ordinality as b(v_value, idx) 
    on a.idx = b.idx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM