根据条件在查询中计算

Calculation in query based on the condition

我在 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;

客户table列:

顺序table列:

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

我也不确定,因为你没有提供足够的信息,但你需要使用 CASE 表达式

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 文档