首页 > 解决方案 > What is wrong with my Postgres statement?

问题描述

SELECT first_name, last_name, manager_id
    CASE manager_id
        WHEN manager_id IS null THEN "pip"
        End manager_id
    FROM assgnssql.employees;

I am trying to select list of employees, but i know some employees do not have manager_id, for these employees without manager_id (null) i want the result to display "pip" while for the rest it displays original info.

标签: sqlpostgresql

解决方案


The code you want is probably:

SELECT first_name, last_name, manager_id
        (CASE WHEN manager_id IS null THEN 'pip' ELSE manager_id
         END) as manager_id
FROM assgnssql.employees;

Or more simply:

SELECT first_name, last_name, manager_id
       COALESCE(manager_id, 'pip') as manager_id
FROM assgnssql.employees;

The two significant issues are:

  • Your CASE syntax is messed up. Either you use comparisons or you have CASE <value>, but not both.
  • Strings are delimited by single quotes.

推荐阅读