I’m trying to build query that return 0 or more and return -1 if Id isn’t exists
For example:
Select Count(*)
From my table
Where id = inputId
I tried to use case like this:
Select
CASE WHEN Count(*) >= 0 THEN Count(*)
ELSE -1 END
From .....
Where....
my issue is that 0 is valid result that mean Id exist with 0 columns,
I need to return -1 if id isn’t exists
>Solution :
Use a case expression to:
- If there are values in that other column, return the count.
- If no value in that other column, return 0 if id still exists.
- None of the above exist, return -1.
select case when count(othercolumn) > 0 then count(id) -- or perhaps "then count(othercolumn)"
when count(id) > 0 then 0
else -1
end
From my table
Where id = inputId