I think part of it is that you're stating the value you're selecting after CASE
, and then using WHEN x = y
syntax afterward, which is a combination of two different methods of using CASE
. It should either be
CASE X
WHEN a THEN ...
WHEN b THEN ...
or
CASE
WHEN x = a THEN ...
WHEN x = b THEN ...