0
我有一個varchar列,其中包含值,我想提取的關鍵財務值,並忽略像測試,abc的單詞。如何計算在列中由管道在postgresql分隔的列中的鍵值對清除值
「其他:爲Transaction1 |金融:保險|理財:保險|其他:地點|金融:測試|金融:ABC」
輸出:保險
請您能有所幫助。
我有一個varchar列,其中包含值,我想提取的關鍵財務值,並忽略像測試,abc的單詞。如何計算在列中由管道在postgresql分隔的列中的鍵值對清除值
「其他:爲Transaction1 |金融:保險|理財:保險|其他:地點|金融:測試|金融:ABC」
輸出:保險
請您能有所幫助。
with temp as (
select unnest(string_to_array(t.str,'|')) as str
from (values('Other:transaction1|finance:insurance|finance:insurance|Other:Place|finance:testing|finance:abc')) as t(str)
)
select distinct substring(str from position(':' in str)+1 for length(str))
from temp
where str ~'finance:' and --matching finance
str !~'testing' and -- ignore words testing
str !~'abc' -- ignore words abc
一種方法是使用regexp_matches
,這樣的事情:
with the_table(id, col) as(
select 1, 'Other:transaction1|finance:testing|finance:testing|finance:Place|finance:testing|finance:insurance'::text union all
select 2, 'finance:insurance'::text union all
select 3, 'ffinance:insurance|finance:testing'::text union all
select 4, 'Other:transaction1|finance:insurance|finance:insurance|Other:Place|finance:testing|finance:abc'::text
)
select distinct id, arr[1] as word from (
select id, regexp_matches('|'||col, '\|finance:([^\|]+)', 'g') as arr
from the_table
) t
where
arr[1] not in('testing', 'abc') -- there is words you don't want to see in result
order by id
這給除「黑名單」作爲不同的行每個單詞。我使用獨特的列id
只是爲了顯示,哪些字對應哪一行。
你說你想'忽略像測試,abc'這樣的詞。如果'保險'字以外的'金融'鍵也有價值'bla',那麼期望的結果是什麼?在這種情況下你想要兩個詞('保險'和'bla')嗎? –