2016-07-11 3 views
0

私はpostgresバージョン8.3(greenplum)を使用しています。私はcol_nameと呼ばれる単一の列の2つのテーブルを比較しようとしています。私が必要とするのは、両方の列の行の値を比較する部分文字列です。値はxx.yyy.zzzです。私は最初の部分、すなわち 'xx'を抜き出し、その後に '.yyy.zzz'を切り捨てたい。私は最初の期間に遭遇するまで、文字列の2つの行を比較したいだけです。文字列xxの部分の長さが文字数で変化する可能性があります。postgres string compare

私は、次のロジックを使用していますが、それは動作していない理由を私は見カント:

select 
distinct x.col_name, 
x.col_num 
from table_A x 
left outer join table_b y 
on 
regexp_matches((x.col_name,'^(?:([^.]+)\.?){1}',1),(y.col_name,'^(?:([^.]+)\.?){1}', 1)) 
and x.col_num=y.col_num; 

私はこのエラーを取得しています:

ERROR: function regexp_matches(record, record) does not exist LINE 36: regexp_matches((x.col_name,'^(?:([^.]+).?){1}', 1),(y.... ^HINT: No function matches the given name and argument types. You may need to add explicit type casts. ********** Error **********

ERROR: function regexp_matches(record, record) does not exist SQL state: 42883 Hint: No function matches the given name and argument types. You may need to add explicit type casts. Character: 917

誰も私を助けることはできますか?

ありがとうございます!

答えて

1

split_part機能を使用できます。区切り文字として'.'を使用して文字列を分割し、最初の要素を比較します。
documentation

を参照してくださいだからあなたのクエリは次のようになります。あなたはregexp_matches機能には奇妙なパラメータを与えるため

select 
    distinct x.col_name, 
    x.col_num 
from table_A x 
    left outer join table_b y 
    on split_part(x.col_name, '.', 1) = split_part(y.col_name, '.', 1) 
    and x.col_num=y.col_num; 

あなたの元のクエリはエラーを生成します。
署名はregexp_matches(string text, pattern text [, flags text])ですが、最初の引数は(x.col_name,'^(?:([^.]+)\.?){1}',1)で、文字列ではありません(第2引数でも同じです)