I have about the following query:
SELECT *
FROM Product
ORDER BY (SELECT CASE
(SELECT MIN(date) FROM SomethingElse WHERE productId = Product.id) AS here
WHEN NULL
THEN 0
ELSE here
) DESC
However, AS in CASE doesn’t work. I need a way to save the value in a variable and use it again.
So you are trying to do
CASE when expression IS NULL THEN 0 ELSE expression END?In this case you can just use
COALESCE(expression, 0)