基于条件的查询计算

时间:2016-01-07 16:40:24

标签: oracle stored-procedures package conditional

我在oracle中有一个包含几个程序,在其中一个程序中我需要根据具体情况计算一些费用如下:

if X_flag = 1 then fee = (.5 * orders.total_count) + (.3 * orders.total_amount) else fee = (.7 * orders.total_count) + (.4 * orders.total_amount)

那么,有什么更好的方法呢?

我需要在其上添加此计算的过程:

procedure informatonRPT ( 
p_customerID in number, 
p_orderID in number)
as
begin 
    select     
        cusromers.costomerId,
        cusromers.costomerName,
        cusromers.coustomerPhone,
        orders.price 

    from       cusromers 
    inner join orders
            on cusromers.orderId = orders.orderID     
    when 
        p_customerID is null or p_customerID = cusromers.costomerId 
        and p_orderID is null or p_orderID = orders.orderID ;

end informatonRPT;

客户表格列:

  • customerID
  • custumerName
  • customerPhone
  • ordedID

订单表列:

  • orderID
  • TOTAL_AMOUNT
  • TOTAL_COUNT

请注意: 费用列在数据库中不存在,我必须在查询中计算它

1 个答案:

答案 0 :(得分:0)

再次我不确定,因为你没有提供enought信息,但你需要使用CASE expresion

SELECT C.costomerId,
       C.costomerName,
       C.coustomerPhone,
       O.price,
       CASE X_flag 
          WHEN 1 THEN (.5 * O.total_count) + (.3 * O.total_amount) 
          ELSE 7 * O.total_count) + (.4 * O.total_amount)
       END as fee
FROM cusromers C
join orders O
  on C.orderId = O.orderID     

oracle CASE 文档