1

I am having two arrays. Both array calculated from functions so both arrays are dynamic but length of both arrays will be same.

a1= ARRAY[1,2,3];
a2= ARRAY[10,20,30];

Now I want to update my table something like this

UPDATE TABLE SET data= CASE 
     data=a1[1] then a2[1]
     data=a1[2] then a2[2]
     data=a1[3] then a2[3]END
where id=1;

I tried with adding loop inside CASE but it is not working .

2
  • Please edit your question and add some sample data and the expected output based on that data. Formatted text please, no screen shots. (edit your question - do not post code or additional information in comments) Commented May 30, 2018 at 12:30
  • Case expression. Commented May 30, 2018 at 12:44

1 Answer 1

1

You can make use of array_position to find the matching index in array 1, and query array 2 using this index:

UPDATE TABLE 
SET data = a2[array_position(a1, data)]
WHERE id = 1;

http://rextester.com/CBJ37276

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.