无法计算成本金额

时间:2020-09-06 07:56:56

标签: sql oracle

CREATE TABLE Purchase_payment
(  
     supplier_id int 
         foreign key references purchase_supplier_master_details(supplier_id)
                 on delete cascade
                 on update cascade,
     stock_name varchar(25),
     QUANTITY INT NOT NULL,
     Currency_type VARCHAR(15),-----FOR BOTH NATIONAL AND INTERNATIONAL SUPPLIER
     COSTING_Amount INT NOT NULL,
     INTERVALS INT,
     PERMONTH_PAYMENTS INT,
     PAYMENT_MODE VARCHAR(25) NOT NULL,
     PAID VARCHAR(5),

     CONSTRAINT PAID CHECK (PAID = 'YES' OR PAID = 'NO')
)

我的查询:

select 
    supplier_id,
    stock_name,
    QUANTITY,
    Currency_type,
    COSTING_Amount as "Discounted_amount",
    case
       when  supplier_id= 41256 and Currency_type ='RUPEE' 
          then COSTING_Amount=(COSTING_Amount-0.25*COSTING_Amount) as "Discounted_amount",
       when supplier_id=41257 and Currency_type='RUPEE'
          then COSTING_Amount=(COSTING_Amount-0.50*COSTING_Amount) as "Discounted_amount"
       else 'no discount'
    end 
from Purchase_payment

此查询仅在货币为卢比时才有效。在购买表中还存在欧元和英镑,因此对于不同的情况,您可以在单个案例中添加所有货币换算吗?

2 个答案:

答案 0 :(得分:1)

您的大小写语法错误,这就是为什么无法计算成本金额的原因

select supplier_id,stock_name,
       QUANTITY,Currency_type,COSTING_Amount,
       case 
          when  supplier_id= 41256 and Currency_type ='RUPEE'  then COSTING_Amount-0.25*COSTING_Amount
          when supplier_id=41257 and Currency_type ='RUPEE'  then COSTING_Amount-0.50*COSTING_Amount 
          else 0 
       end as "Discounted_amount"
from Purchase_payment

答案 1 :(得分:0)

您的数据模型似乎有问题。像这样对供应商ID进行硬编码是不好的做法。利率也可能如此。您应该做的是拥有一个单独的表来保存值。像这样:

CREATE TABLE supplier_discount
(  
     supplier_id int 
         foreign key references purchase_supplier_master_details(supplier_id)
                 on delete cascade,
     Currency_type VARCHAR(15),
     discount_rate number);

使用外部联接在查询中包含此表(以处理对于给定货币没有折扣的供应商)。请注意,在同一列中返回数字和字符串也是一种不好的做法,因此,当没有折扣时,我的CASE语句将返回零。

select 
    pp.supplier_id,
    pp.stock_name,
    pp.quantity,
    pp.currency_type,
    pp.costing_amount,
    case
       when sd.discount_rate is not null then 
       then pp.costing_amount - (sd.discount_rate * pp.costing_amount)
       else 0 
       end as discounted_amount
from purchase_payment pp
left outer join supplier_discount sd
   on sd.supplier_id = pp.supplier_id
   and sd.currency_type = pp.currency_type