0

I have a column which is a string of comma seperated elements like this statuses= 'Initial,Initial,Completed,InProgress. And I what to make a status column like this:

CASE
    WHEN <all statuses are 'Initial'> THEN 'Initial'
    WHEN <all statuses are 'Completed'> THEN 'Completed'
    ELSE 'InProgress'
END AS status

I have tried bool_and(string_to_array(statuses,',')='Initial'), which does not compile. Any suguestions?

1 Answer 1

1

You could do this with string_to_array() and the ALL operator:

case 
    when 'Initial'   = ALL(string_to_array(statuses, ',')) then 'Initial'
    when 'Completed' = ALL(string_to_array(statuses, ',')) then 'Completed'
    else 'InProgress'
end status

Demo on DB Fiddle:

with t as (
    select 'Initial,Initial' statuses
    union all select 'Completed,Completed'
    union all select 'Initial,Completed,Other'
)
select 
    statuses,
    case 
        when 'Initial'   = ALL(string_to_array(statuses, ',')) then 'Initial'
        when 'Completed' = ALL(string_to_array(statuses, ',')) then 'Completed'
        else 'InProgress'
    end status
from t
statuses                | status    
:---------------------- | :---------
Initial,Initial         | Initial   
Completed,Completed     | Completed 
Initial,Completed,Other | InProgress
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.