I have a table with multiple rows, each of which contains these four columns:
| product | price_instore | price_pickup | price_delivery | price_ship |
|---|---|---|---|---|
| 1 | $13.00 | $13.50 | $14.50 | $18.00. |
| 2 | $4.00 | $4.00 | NULL | NULL |
| 3 | $10.00 | $10.00 | $12.00 | NULL |
I'd like to have a fifth column average_price that gives the average price for a product, but does not count NULLS towards the sum price, or towards the count used to divide the average.
So average price of product 1: ($13+$13.50+$14.50+$18)/4=$14.75
Average price of product 2: ($4+$4)/2 = $4.00
Average price of product 3: ($10+$10+$12)/3 = $10.67
Is there any way to do this is SQL? I've tried subqueries such as the one below without success:
(select coalesce((PRICE_INSTORE + PRICE_PICKUP + PRICE_DELIVERY + PRICE_SHIP) / 4,
PRICE_INSTORE, PRICE_PICKUP, PRICE_DELIVERY, PRICE_SHIP)
but then I only get one price if any of them are null