postgresql中不存在列?

时间:2015-06-17 11:37:54

标签: sql postgresql postgresql-9.1

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

我的查询是:

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 ;

2 个答案:

答案 0 :(得分:3)

类似的东西:

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;

答案 1 :(得分:0)

您可以使用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;