It seems to me that this would be easy, but it turned out to be more difficult than necessary.
Currently, I have several price fields for each record in the table (sale_price, retail_price, discounted_price, other_price), but I would like to get only the highest and lowest non-zero values.
This works fine for the highest value:
SELECT GREATEST(retail_price, sale_price, discounted_price, other_price) AS highest_price FROM TABLE
However, fields in the database that do not have values โโare stored as zeros, so finding the smallest value that is not zero proves complexity. This does not work:
SELECT LEAST(retail_price, sale_price, discounted_price, other_price) AS lowest_price FROM TABLE
when it returns zero.
Is there an easy way to grab a nonzero smallest value from a list with a simple modification to the instruction?
Any help / push in the right direction is appreciated!
Sample data:
stock_number discounted_price sale_price retail_price other_price
1 999 888 0 777
2 55 22 33 11
3 0 0 0 0
:
stock_number highest_price lowest_price
1 999 777
2 55 11
3 0 0