Skip to content
Advertisement

How do I get value corresponding to latest date in group to fill in null values in Oracle SQL?

I have the following table in Oracle SQL:

Notice that there are six rows with NULL values for COLOR_CHANGE_DATE and COLOR. I want to fill those values with the latest color and date based on car. For example, once I fill in the NULL values, I want the resulting table to look like the following:

Note that the NULL values for the six rows have been filled in with the latest COLOR_CHANGE_DATE and COLOR for each CAR. That is, for Ford F150, the NULL values are replaced with the latest COLOR_CHANGE_DATE= 26-Mar-20 and COLOR = brown.

There can be indefinite number of owners that do not have corresponding COLOR and COLOR_CHANGE_DATEs. If an owner does not have COLOR and COLOR_CHANGE_DATEs, then I need to fill in the NULL values with the latest COLOR_CHANGE_DATE and COLOR for each car.

Can you show me how to do this in Oracle SQL?

Below is the SQL query to generate the sample table for your convenience.

Any help would be greatly appreciated!

Advertisement

Answer

Oracle supports ignore nulls in window functions, so you can just do:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement