[英]Return all substring values from a string column when contains a small list of substring
我有一張處方表,同一列中可能包含多種葯物。 我希望僅在存在某些葯物(3 種葯物的列表)時返回查詢,只有一種或任何一種或全部存在。 例如,當有 Medicine A、Medicine B 和 Medicine C 時查找。該字段可能還包含有關該葯物的一些注釋。
輸入:
*column A*
Medicine A
Medicine B and Medicine D
2 x day Medicine A and 1 shot of Medicine C
Medicine E and Medicine D
Medicine D , Medicine A and Medicine B
Medicine E , Medicine C and Medicine A
預計 Output:
*column A*
Medicine A
Medicine B
Medicine A Medicine C
Medicine A Medicine B
Medicine C Medicine A
我正在使用 Postgres 13.7。 任何建議表示贊賞! 謝謝
我試着用 case when 工作。 但是,如果我需要在列表中包含另一種葯物,將會擴展很多。
您可以使用regexp_matches
function 獲取所有必需的匹配項,然后使用string_agg
function 將結果匹配項分組到一行中。
select columnA, string_agg(translate(m::text, '{}"',''), ' ') matches
from
(
select columnA ,
regexp_matches(columnA, 'Medicine [ABC]', 'g') m
from table_name
) T
group by columnA
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.