I want to compare multiple date columns with each other and create a new column "latest_date", which is basically the greatest of them all.
For this we could use the GREATEST()
function (DOC: https://docs.snowflake.com/en/sql-reference/functions/greatest). The problem is that this function's output is null if any of the compared columns is null.
I have read multiple solutions for this here:
- Handling Null in Greatest function in Oracle
- Avoiding NULL return value with GREATEST function
- Greatest not null column
Most of them propose to use the "COALESCE" function as follows:
GREATEST(
COALESCE(a, b, c),
COALESCE(b, c, a),
COALESCE(c, a, b)
)
This is OK if we have just 2 or 3 columns, but does not escalate well when handling many columns (some with them containing nulls).
In my use case, I'm using Snowpark Dataframes (along with Snowflake SQL) and I handle most of these date columns in my tables/dataframes in a "generic way" so I don't know in advance how many columns I will be comparing.
Could someone think of a workaround for this?