I have something like the table below that is incredibly large.
Where the "Category" column is Null, I need to return the Category based on the rows where category is NOT null. The logic should be that IF the Category is NULL and the Sub_Identifier is CONTAINED in the main identifier string, THEN that matching Category should be set for that line item in the row.
Based on the example chart, I’m expecting something like this to return.
The part where I am stuck is related to returning/setting the matches in the Category column for the Sub_Identifiers.
2
Answers
A cleaner and easier to understand approach to solve this –
Firstly, we update the "Category" column in "YourTable" table (aliased as t1) and then; using a self-join with another instance of the "YourTable" table (aliased as t2), we’re checking for rows where the "Category" in the target row (t1) is null and the "Category" in the source row (t2) is not null. We’re also using the
POSITION
function to check if the "Sub_Identifier" in the source row (t2) is contained within the "Main_Identifier" of the target row (t1). If the conditions are met, we’re updating the "Category" in the target row with the "Category" from the source row.