I am trying case inside select query and want to use that data column is generated by that case in same query.
My query is :
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 ;
Something like:
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;
You can DRY up the duplication of the projection with a CTE
, and then use this in your WHERE
predicate:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.