i wrote a query to return the max value of multiple columns and then to return the name of the column with the max value, the query seems to be working except for 2 things it should exclude anything where the ‘qty’ column associated with the value column is <1 and it is not, so that cause the second issue where if 2 columns have the same value and they are the highest then it returns the column name that appears first instead of the column name that is associated with the ‘qty’ column >0 here is the query
SELECT order_qty, po, ShipDate, Status,
LEAST( IF (ABCNewPrice > 0 AND ABCQty > 0, ABCNewPrice, 999999),
IF(ANewPrice>0 AND AQty>0 ,ANewPrice,999999),
IF(AUsedPrice>0 AND AUsedQty>0,AUsedPrice,999999))
as theMin,
CASE LEAST(
IF (ABCQty > 0, ABCNewPrice, 999999),
IF(AQty>0 ,ANewPrice,999999),
IF(AUsedQty>0,AUsedPrice,999999))
WHEN ABCNewPrice THEN ‘ABCNewPrice’
WHEN ANewPrice THEN ‘ANewPrice’
WHEN AUsedPrice THEN ‘AUsedPrice’
END AS LeastVarColumn
FROM cabs