Dan
Dan

Reputation: 4663

Select value dependent on other column value

I want the query to return the distance if the user has opted to show their position. This is what I have:

SELECT 
  showpos, 
  CASE distance WHEN showpos=1 THEN distance ELSE "N/A" END 
FROM table

I also have a case statement for showpos (1=True/0=False) but this does not have any errors. This is not returning the correct results. How do I make it show distance only when showpos=1 and return "N/A" when showpos=0?

Original data:

original data

After Query Results:

returned data

Upvotes: 0

Views: 204

Answers (1)

CL.
CL.

Reputation: 180260

SELECT showpos, 
       CASE WHEN showpos = 1 THEN distance ELSE 'N/A' END
FROM mytable

or

SELECT showpos, 
       CASE showpos WHEN 1 THEN distance ELSE 'N/A' END
FROM mytable

Upvotes: 2

Related Questions