sql - Trim left non ascending elements from array -


i have table in postgresql-9.3 integer array column. need trim arrays such keep elements starting end of array ascending.

example array: {2,6,8,3,4,5,8,9,11,3,5,7}

trimmed: {3,5,7}

the code not need fast or pretty, gets run once fix bad data.

i did not expect manage, did solve myself:

select arr[(select coalesce(max(row_number)+1,1) (     select unnest, lead(unnest) over(), row_number() on () (         select unnest(arr) test id=c.id     ) ) b unnest>lead):array_upper(arr,1)]  test c; 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -