使用 case 语句查看在列上的连接

View join on column with case statement

我有一个使用 case 语句的列的视图。有没有办法可以将这个动态值(列:健康)与另一个 table?

Table 产品

| id | status |
| -- | ------ | 
| 1  | OK     |
| 2  | STABLE |
| 3  | FAILED |
| 4  | ABORT  |

TABLE 产品详情

| health | score |
| ------ | ----- |
| GOOD   | 100   |
| FAIR   | 80    |
| POOR   | 60    |

使用 productDetail 动态创建加入此列 (health) 的视图不起作用

CREATE OR REPLACE VIEW MyView AS
   SELECT
   product.id,
   product.status,
   CASE
       WHEN product.status='OK' THEN 'GOOD'
       WHEN product.status='STABLE' THEN 'FAIR'
       ELSE 'POOR'
   END AS health,
   pd.score
   FROM Product product
   LEFT JOIN ProductDetail pd ON health = pd.health;

我的实际案例栏很复杂,它检查了多个字段。感谢任何提示。

将您的 select 从产品转变为 本地 table,方法是将其设为子 select 创建 health一路上的专栏。然后加入产品详情。 (参见 demo

create or replace view MyView as                 
    select id, status, pd.health                        
      from (                                            
             select product.id, product.status,         
                case                                             
                    when product.status='OK'    then 'GOOD'         
                    when product.status='STABLE'then 'FAIR'     
                    else 'POOR'                                  
                end as health 
               from product 
           ) p
       left join ProductDetail pd ON pd.health = p.health;