0

I have this table whose null values need to be populated using values from val2 within a group (val1). However, I want to populate these nulls while taking into consideration name1 and/or name2 fields.

HAVE

val1 val2  name1 name2
1    abc    Jon   Doe
1   (null)  Jony  Doe
1   bde     Jony  Doe
1   abc     Jon   Doe
2   x1y2    Jony  Ronald
2   x1y2    Jony  Ronald
2  (null)   Jony  Ronald 
2  (null)   Jony  Ronald


WANT

val1 val2  name1 name2
1   abc     Jon   Doe
1   bde     Jony  Doe
1   bde     Jony  Doe
1   abc     Jon   Doe
2   x1y2    Jony  Ronald
2   x1y2    Jony  Ronald
2   x1y2    Jony  Ronald 
2   x1y2    Jony  Ronald

2 Answers 2

1

One option would be using MAX(val2) OVER (PARTITION BY name1, name2 ORDER BY ...) analytic function whenever values of the column val2 are null :

SELECT val1,
       NVL(val2,MAX(val2) OVER (PARTITION BY name1, name2 ORDER BY 0)) AS val2,
       name1, name2
  FROM t

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a correlated subquery to retrieve the values you need when it's null.

For example:

update my_table a
set val2 = (
  select max(b.val2) 
  from my_table b 
  where b.name1 = a.name1 and b.name2 = a.name2
)
where val2 is null

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.