Click here to Skip to main content
15,882,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Current data:

| Old_code | New_code |
| -------- | -------- |
| HBP112 | HBP0501 |
| HBP112 | NULL |
| HBP132 | HBP0501 |
| HBP132 | NULL |
| HCIH112 | NULL |
| HCIH112 |HCIH0501 |


Required output:


| Old_code | New_code | Required Code|
-------- | -------- | ----------------|
| HBP112 | HBP0501 | HBP0501 | |
| HBP112 | NULL | HBP0501 |
| HBP132 | HBP0501 | HBP0501 |
| HBP132 | NULL | HBP0501 |
| HCIH112 | NULL | HCIH0501 |
| HCIH112 |HCIH0501 | HCIH0501 |

What I have tried:

select *, Max(new_code) over(partition by old_code) Required_code
from t;

i am still getting null values in result
Posted
Updated 2-Aug-23 0:09am
Comments
Member 15627495 2-Aug-23 6:18am    
when you use MAX(column_A) , you have to add "group by column_A"

1 solution

You're nearly there:
SQL
SELECT
    Old_Code,
    New_Code,
    IsNull(MAX(New_Code) OVER (PARTITION BY Old_Code), Old_Code) As Required_Code
FROM
    t
;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900