2016-08-31 16 views
1

文字列の区切り文字に基づいて文字列をn列に分割する方法はありますか。私は、3つの引数、文字列、区切り文字、および文字列のn番目の区切り文字があるSPLIT_PART関数を認識しています。例:文字列を別々の列に分割するためのVertica SQL関数

select 
    split_part('2016-01-01 00:11:00|Sprout|0', '|', 1), split_part('2016-01-01 00:11:00|Sprout|0', '|', 2), split_part('2016-01-01 00:11:00|Sprout|0', '|', 3); 

あなただけの文字列と区切り文字を供給するだろうし、あなたが区切り文字があなたの文字列で表示されますしかし、多くの列で終わるだろう第三引数なしでこれを行う方法はありますか?

一度VerticaがPythonベースのUDFを使用できるようになると、これは.split()メソッドを使用した簡単な修正ですが、現在の解決策はありますか?私はこれが長い間起こっている可能性が高いことを知っていますが、私はsplit_partを使うことが私の目的のために完全に機能するので、好奇心からほとんどを求めています。

これが可能であることは[OK]を許容答え

答えて

1

なりません。あなただけの文字列のn番目のトークンを取得させていただきます場合は、試してみてください。

SQL>SELECT 
    ...> regexp_substr(
    ...> '2016-01-01 00:11:00|Sprout|0' -- source string 
    ...> , '[|]?([^|]+)' -- pattern (an optional bar, followed by many non-bars, which we remember as the 1st group) 
    ...> , 1    -- starting from begin of string: position 1 
    ...> , 1    -- the N-th occurrence 
    ...> , ''   -- no regexp modifier 
    ...> , 1    -- we want the only remembered group - the 1st 
    ...> ) the_first 
    ...>, regexp_substr(
    ...> '2016-01-01 00:11:00|Sprout|0' -- source string 
    ...> , '[|]?([^|]+)' -- pattern (an optional bar, followed by many non-bars, which we remember as the 1st group) 
    ...> , 1    -- starting from begin of string: position 1 
    ...> , 2    -- the N-th occurrence 
    ...> , ''   -- no regexp modifier 
    ...> , 1    -- we want the only remembered group - the 1st 
    ...> ) the_second 
    ...>, regexp_substr(
    ...> '2016-01-01 00:11:00|Sprout|0' -- source string 
    ...> , '[|]?([^|]+)' -- pattern (an optional bar, followed by many non-bars, which we remember as the 1st group) 
    ...> , 1    -- starting from begin of string: position 1 
    ...> , 3    -- the N-th occurrence 
    ...> , ''   -- no regexp modifier 
    ...> , 1    -- we want the only remembered group - the 1st 
    ...> ) the_third 
    ...>; 
    the_first     |the_second     |the_third 
    2016-01-01 00:11:00   |Sprout      |0 

をしかし、あなたはあなたの区切られた文字列を旋回するようにしたい場合は、各トークンは、新しいラインを形成するように - 二つの可能性:

SQL>-- manual, using regexp_substr ... 
    ...>with 
    ...>the_array as (
    ...>   select 1 as idx 
    ...>union all select 2 
    ...>union all select 3 
    ...>union all select 4 
    ...>union all select 5 
    ...>union all select 6 
    ...>union all select 7 
    ...>union all select 8 
    ...>union all select 9 
    ...>union all select 10 -- increase if you might get a bigger array than one of 10 elements 
    ...>) 
    ...> ,concepts as (
    ...>select '2016-01-01 00:11:00|Sprout|0' as concepts_list 
    ...>) 
    ...>select * from (
    ...> select 
    ...> idx 
    ...> ,trim(
    ...> regexp_substr(
    ...>  concepts_list -- source string 
    ...> ,'[|]?([^|]+)' -- pattern (an optional bar, followed by many non-bars, which we remember as the 1st group) 
    ...> ,1    -- starting from begin of string: position 1 
    ...> ,idx   -- the idx-th occurrence 
    ...> ,''   -- no regexp modifier 
    ...> ,1    -- we want the only remembered group - the 1st 
    ...> ) 
    ...> ) as concept 
    ...> from concepts 
    ...> cross join the_array 
    ...>) foo 
    ...>where concept <> '' 
    ...>; 
    idx     |concept 
         1|2016-01-01 00:11:00 
         3|0 
         2|Sprout 
    select succeeded; 3 rows fetched 
    SQL>-- using the strings_package on: 
    ...>-- https://github.com/vertica/Vertica-Extension-Packages/blob/master/strings_package/src/StringTokenizerDelim.cpp 
    ...>WITH csvtab(id,delimstring) AS (
    ...>   SELECT 1,'2016-01-01 00:11:00|Sprout|0' 
    ...>UNION ALL SELECT 2,'2016-01-02 00:11:00|Trout|1' 
    ...>UNION ALL SELECT 3,'2016-01-03 00:11:00|Salmon|2' 
    ...>UNION ALL SELECT 4,'2016-01-04 00:11:00|Bass|3' 
    ...>) 
    ...>SELECT id, words 
    ...>FROM (
    ...> SELECT id, v_txtindex.StringTokenizerDelim(delimstring,'|') OVER (PARTITION by id) FROM csvtab 
    ...>) a 
    ...>ORDER BY 1; 
    id     |words 
         1|2016-01-01 00:11:00 
         1|Sprout 
         1|0 
         2|2016-01-02 00:11:00 
         2|Trout 
         2|1 
         3|2016-01-03 00:11:00 
         3|Salmon 
         3|2 
         4|2016-01-04 00:11:00 
         4|Bass 
         4|3 
    select succeeded; 12 rows fetched 
+0

私は、select文に3つの項目を持たずに別々の列を得ることができるのだろうかと思っていました。 Pythonに慣れているなら、string.split( '|')の効果が欲しいです。 SQLでこれができない場合は完全に問題ありません。あなたの最初の例は、私がおそらくvertica関数SPLIT_PART(文字列、区切り文字、occurence)を使って行くルートです。 – mangodreamz

関連する問題