如何添加根据一组条件变化的折扣列?

时间:2019-06-24 11:38:36

标签: sql oracle

我想添加折扣列,它根据帐户类型和服务而有所不同。折扣列不存在于任何表中。

我希望在运行时创建一个列折扣列,而折扣根据Resller_nameAccount_type和服务而变化。

如何添加折扣栏?

我还需要使用以下公式添加计算成本列:  a.list_cost *(1-d。折扣)。我尝试创建表折扣并添加异常值,并在脚本下方创建:

select a.account, a.name, b.Resller_Name, b.bcn, 
b.Account_Type as "Internal\reseller", a.service, a.list_cost "cost of service",
d.discount, a.list_cost*(1-d.discount) as "Calculated Cost" 
from imtest.cloudchckr_test_full a, imtest.master_info_test_full b, imtest.discount d 
where a.account=b.AWS_id(+) and b.Account_type=d.account_type; 

但是它仅显示基于帐户类型的折扣。

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以在折扣表或折扣表中的任何唯一ID上添加订单,然后将其加入所有表并获得折扣金额。如果折扣数据不存在,请使用折扣表左联接。

答案 1 :(得分:0)

select 
   a.account, 
   a.name, 
   b.Resller_Name, 
   b.bcn, 
   b.Account_Type as "Internal\reseller", 
   a.service, 
   a.list_cost "cost of service", 
   d.discount, 
   a.list_cost*(1-d.discount) as "Calculated Cost" 
from imtest.cloudchckr_test_full a, 
   imtest.master_info_test_full b, 
   imtest.discount d 
where 
   a.account=b.AWS_id(+) 
   and (d.account_type is null or b.Account_type=d.account_type)
   and (d.Resller_Name is null or b.Resller_Name=d.Resller_Name)
   and (d.service is null or a.service=d.service)
UNION ALL
select 
   a.account, 
   a.name, 
   b.Resller_Name, 
   b.bcn, 
   b.Account_Type as "Internal\reseller", 
   a.service, 
   a.list_cost "cost of service", 
   0, 
   a.list_cost as "Calculated Cost" 
from imtest.cloudchckr_test_full a, 
   imtest.master_info_test_full b
where 
   a.account=b.AWS_id(+) 
   and not exists 
        (SELECT 1 
             FROM imtest.discount d 
          WHERE (d.account_type is null or b.Account_type=d.account_type)
            and (d.Resller_Name is null or b.Resller_Name=d.Resller_Name)
            and (d.service is null or a.service=d.service)
         )