postgresql 中不存在该列?

Column does not Exist in postgresql?

我正在尝试 select 查询中的案例,并希望在同一查询中使用该案例生成的数据列。

我的查询是:

select order_id , order_item_id , sku ,merchant_payable as "Value Get" , 

case when name like 'Rise%'
    then amount-(((amount*12.14)/100)+ ((amount*3.08)/100) + 51.30)
    when name like 'Masha%'
    then amount-(((amount*9.10)/100)+ ((amount*3.08)/100) + 51.30)
    when name like 'Bboy%'
    then amount-(((amount*14.17)/100)+ ((amount*3.08)/100) + 51.30)
    end as "Value Should Get" ,

 "Value Should Get"  - merchant_payable 
from meta.paytm_payment as ppo  
where
 case when name like 'Rise%'
    then amount-(((amount*12.14)/100)+ ((amount*3.08)/100) + 51.30)
    when name like 'Masha%'
    then amount-(((amount*9.10)/100)+ ((amount*3.08)/100) + 51.30)
    when name like 'Bboy%'
    then amount-(((amount*14.17)/100)+ ((amount*3.08)/100) + 51.30)
    end > merchant_payable

 order by order_created_at ;

类似于:

SELECT  order_id ,
        order_item_id ,
        order_created_at ,
        sku ,
        "Value Get" ,
        "Value Should Get" ,
        "Value Should Get" - merchant_payable
FROM    ( SELECT    order_id ,
                    order_item_id ,
                    order_created_at ,
                    sku ,
                    merchant_payable AS "Value Get" ,
                    CASE WHEN name LIKE 'Rise%'
                         THEN amount - ( ( ( amount * 12.14 ) / 100 )
                                         + ( ( amount * 3.08 ) / 100 ) + 51.30 )
                         WHEN name LIKE 'Masha%'
                         THEN amount - ( ( ( amount * 9.10 ) / 100 )
                                         + ( ( amount * 3.08 ) / 100 ) + 51.30 )
                         WHEN name LIKE 'Bboy%'
                         THEN amount - ( ( ( amount * 14.17 ) / 100 )
                                         + ( ( amount * 3.08 ) / 100 ) + 51.30 )
                    END AS "Value Should Get"
          FROM      meta.paytm_payment AS ppo
        ) t
WHERE   "Value Should Get" > merchant_payable
ORDER BY order_created_at;

您可以使用 CTE 干掉投影的副本,然后在您的 WHERE 谓词中使用它:

WITH myCte AS
(
    select order_id , order_item_id , sku ,merchant_payable, order_created_at ,
    case when name like 'Rise%'
        then amount-(((amount*12.14)/100)+ ((amount*3.08)/100) + 51.30)
        when name like 'Masha%'
        then amount-(((amount*9.10)/100)+ ((amount*3.08)/100) + 51.30)
        when name like 'Bboy%'
        then amount-(((amount*14.17)/100)+ ((amount*3.08)/100) + 51.30)
    end as "Value Should Get"
    from paytm_payment as ppo  
)
SELECT *, "Value Should Get"  - merchant_payable AS "Nett"
from myCte
where "Value Should Get" > merchant_payable
order by order_created_at;