RobotRock
RobotRock

Reputation: 4459

CASE with AS query

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.

Upvotes: 1

Views: 189

Answers (1)

Martin Smith
Martin Smith

Reputation: 453328

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)

Upvotes: 4

Related Questions