regex - Extracting Values from Array in Redshift SQL -
i have arrays stored in redshift table "transactions" in following format:
id, total, breakdown 1, 100, [50,50] 2, 200, [150,50] 3, 125, [15, 110] ... n, 10000, [100,900]
since format useless me, need processing on values out. i've tried using regex extract it.
select regexp_substr(breakdown, '\[([0-9]+),([0-9]+)\]') transactions
but error returned says
unmatched ( or \( detail: ----------------------------------------------- error: unmatched ( or \( code: 8002 context: t_regexp_init query: 8946413 location: funcs_expr.cpp:130 process: query3_40 [pid=17533] --------------------------------------------
ideally x , y own columns can appropriate math. know can easy in python or php or like, i'm interested in pure sql solution - partially because i'm using online sql editor (mode analytics) plot dashboard.
thanks help!
if breakdown
array can this:
select id, total, breakdown[1] x, breakdown[2] y transactions;
if breakdown not array e.g. varchar
column, can cast array if replace square brackets curly braces:
select id, total, (translate(breakdown, '[]', '{}')::integer[])[1] x, (translate(breakdown, '[]', '{}')::integer[])[2] y transactions;
Comments
Post a Comment