I'm currently using the COALESCE
function to grab the first NOT NULL value from a list of fields. Example:
COALESCE(header_to_node_13.subsetname, header_to_node_12.subsetname,
header_to_node_11.subsetname, header_to_node_10.subsetname,
header_to_node_9.subsetname, header_to_node_8.subsetname,
header_to_node_7.subsetname, header_to_node_6.subsetname,
header_to_node_5.subsetname, header_to_node_4.subsetname,
header_to_node_3.subsetname, header_to_node_2.subsetname,
header_to_node_1.subsetname,
header_to_node.subsetname, header_to_node.setname) AS prctr1
I need to modify the logic so that I grab the value to the right of the COALESCE
value as well, so the two values can be compared to populate a newly created field.
For instance, if the COALESCE
value is header_to_node_5.subsetname
I need to grab header_to_node_4.subsetname
, as well, so the two can be used in a CASE
statement.
If the second value (header_to_node_4.subsetname
) IS NULL I want the first value (header_to_node_5.subsetname
). If the second value is NOT NULL I want to use the second value.
I'm stumped on how to grab the second value. I am using Greenplum/PostgreSQL 8.2