2016-04-28 89 views
0

我想向我的代碼添加以下邏輯(這是巨大的,沒有這個工作,所以必須是下面的代碼有問題),但我得到一個錯誤。我懷疑,什麼是錯與END但在邏輯思維所有3應該是在年底,所以我不知道多個案例陳述不工作

case 
    when t0."Final Sale Price" is null 
     then null 
    else case 
     when t1."Fee" is not null 
     and t0."Final Sale Price" >= t1."Guaranteed Price" 
     then t1."Fee" 
     else case 
     when t1."Fee" is not null 
     and t0."Final Sale Price" <= t1."Guaranteed Price" 
      then t1."Fee" - (t1."Guaranteed Price" - t0."Final Sale Price") 
     else case 
      when t0."Final Sale Price" < t1."Guaranteed Price" 
      then t0."Final Sale Price" - t1."Guaranteed Price" 
      else(
      (
       (t0."Final Sale Price" - t1."Guaranteed Price") * (100 - t1."Seller Upside %") 
      ) 
      /100 
     ) 
     end 
     end 
    end as "Actual Revenue" 
+1

我算了四個案例,但只有三個結局 – David784

+0

而且我只能找到案例表達式,而不是單個案例語句。 – jarlh

回答

0
case 
when t0."Final Sale Price" is null then null 
when t1."Fee" is not null then 
    case when t0."Final Sale Price" >= t1."Guaranteed Price" then t1."Fee" 
      when t0."Final Sale Price" < t1."Guaranteed Price" then t1."Fee" - (t1."Guaranteed Price" - t0."Final Sale Price") 
    end 
when t0."Final Sale Price" < t1."Guaranteed Price" then t0."Final Sale Price" - t1."Guaranteed Price" 
else ((t0."Final Sale Price" - t1."Guaranteed Price") * (100 - t1."Seller Upside %"))/100 
end as "Actual Revenue" 

表達式可以簡化如上。

另外要注意的問題有2個條件,

t0."Final Sale Price" >= t1."Guaranteed Price"

t0."Final Sale Price" <= t1."Guaranteed Price"

最終銷售價格=保證價格在這兩個條件。如果第一個條件成立,則第二個條件在相同時不會被評估。根據您期望的case表達式的輸出,確保=僅指定一次。

1

一般沒有必要窩case聲明:

select (case when t0."Final Sale Price" is null 
      then null 
      when t1."Fee" is not null and t0."Final Sale Price" >= t1."Guaranteed Price" 
      then t1."Fee" 
      when t1."Fee" is not null and t0."Final Sale Price" <= t1."Guaranteed Price" 
      then t1."Fee" - (t1."Guaranteed Price" - t0."Final Sale Price") 
      when t0."Final Sale Price" < t1."Guaranteed Price" 
      then t0."Final Sale Price" - t1."Guaranteed Price" 
      else (t0."Final Sale Price" - t1."Guaranteed Price") * (100 - t1."Seller Upside %")/100 
     ) 
     end) as "Actual Revenue"