简体   繁体   English

PostgreSQL情况下多列给出错误列不存在

[英]PostgreSQL case multiple columns gives error column does not exist

Is it possible to run a case statement with two different columns in postgresql? 是否可以在postgresql中使用两个不同的列运行case语句?

I'd like to map integers to specific values using a CASE statement. 我想使用CASE语句将整数映射到特定值。 I'm able to do this with ds.ordinal but when I add dcs.status I receive the following error: column "follow_up" does not exist . 我可以使用ds.ordinal做到这一点,但是当我添加dcs.status时,出现以下错误: column "follow_up" does not exist

Is it possible to use multiple case statements on different columns in one SELECT statement? 是否可以在一个SELECT语句的不同列上使用多个case语句?

How can I get case when dcs.status = 0 then "follow_up" end as "follow_up" to not return an error? case when dcs.status = 0 then "follow_up" end as "follow_up"而不返回错误case when dcs.status = 0 then "follow_up" end as "follow_up"如何获得case when dcs.status = 0 then "follow_up" end as "follow_up"

SELECT DISTINCT ON (pd.id)
           case when ds.ordinal = 1 then s.name end as "primary_specialty",
           case when ds.ordinal = 2 then s.name end as "secondary_specialty",
           case when dcs.status = 0 then "follow_up" end as "follow_up"

    FROM potential_doctors AS pd
         INNER JOIN patient_profile_potential_doctors as pppd on pd.id = pppd.potential_doctor_id
         INNER JOIN doctor_taxonomies AS dt on pd.id = dt.potential_doctor_id
         INNER JOIN taxonomies AS t on dt.taxonomy_id = t.id
         INNER JOIN doctor_profiles AS dp on pd.npi = dp.npi
         INNER JOIN doctor_specialties AS ds on dp.id = ds.doctor_profile_id
         INNER JOIN specialties AS s on ds.specialty_id = s.id
         INNER JOIN doctor_creation_notes as dcs on dcs.doctor_profile_id = dp.id

    WHERE dp.approved IS FALSE

I would expect the query to look like this; 我希望查询看起来像这样;

SELECT DISTINCT ON (pd.id)
       (case when ds.ordinal = 1 then s.name end) as primary_specialty,
       (case when ds.ordinal = 2 then s.name end) as secondary_specialty,
       (case when dcs.status = 0 then 'follow_up' end) as follow_up
FROM . . . 
WHERE dp.approved IS FALSE
ORDER BY pd.id;

In other words, I think you just need single quotes around the string constant. 换句话说,我认为您只需要在字符串常量周围加上单引号即可。

I made two other changes. 我做了另外两个更改。 If you are using DISTINCT ON , then you should have an ORDER BY with the expressions in parentheses for the DISTINCT ON as the first keys in the ORDER BY . 如果您使用的是DISTINCT ON ,那么您应该有一个ORDER BY ,括号中的表达式用于DISTINCT ON作为ORDER BY的第一个键。 You can add more keys to get the earliest, latest, biggest, smallest, or whatever row you specifically want. 您可以添加更多键以获取最早,最新,最大,最小或任何您想要的行。

Also, I removed the double quotes from column names. 另外,我从列名中删除了双引号。 You apparently have a confusion with quotes in SQL. 您显然对SQL中的引号感到困惑。 My advice is to use single quotes only for string and date constants. 我的建议是仅对字符串和日期常量使用单引号。 Don't use double quotes, and avoid them by giving your identifiers names using only valid characters. 不要使用双引号,而应通过仅使用有效字符为标识符命名来避免使用双引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM