我正在将SQL别名设置为变量,以替换下面的excel公式
=+IF((O2+Z2)=0,0,+IF(AA2=0,(O2*-1),+IF(AND(AA2>0,(((R2*O2)/(O2+Z2))+H2)<= (O2*-1)),(O2*-1),(((R2*O2)/(O2+Z2))+H2))))
上面是“已付的大笔税收”的公式,但是我需要检查SQL如果AA(平衡)列为零,然后设为0
即自治区税负数,即公式的这一位(IF(AA2 = 0,(O2 * -1))。如果AA(平衡)不为0,则公式有效,但如果为零,则计算不正确
“已付的居民税”公式如下
(Nett Remittance * Borough Tax)/(Borough Tax + Net_W_and_S_1) + (a.amt_benefit)
(((a.amt_remit) + (a.amt_refunds)) * ((a.amt_debit) + (a.amt_costs) + (a.amt_penalties) + (a.amt_write_off) + (a.amt_transitional) + (a.amt_lump_disc))/NULLIF((( (a.amt_debit) + (a.amt_costs) + (a.amt_penalties) + (a.amt_write_off) + (a.amt_transitional) + (a.amt_lump_disc)) + ((a.amt_water)+(a.amt_water_disab_redn)+(a.amt_water_disc)+(a.amt_sewerage)+(a.amt_sewerage_disab_redn)+(a.amt_sewerage_disc)+(a.amt_sewerage_transit)) ) , 0)) + (a.amt_benefit) AS Borough_Tax_Paid,
(a.amt_debit) + (a.amt_costs) + (a.amt_penalties) + (a.amt_write_off) + (a.amt_transitional) + (a.amt_lump_disc) AS Borough_Tax
(a.amt_remit) + (a.amt_refunds) AS Net_Remittance
(a.amt_water)+(a.amt_water_disab_redn)+(a.amt_water_disc)+(a.amt_sewerage)+(a.amt_sewerage_disab_redn)+(a.amt_sewerage_disc)+(a.amt_sewerage_transit) AS Net_W_and_S_1
答案 0 :(得分:1)
使用CASE
语句,例如:
CASE
WHEN Balance = 0 THEN -- some value, or a formula
WHEN Balance < 0 THEN -- maybe some other value, or a formula
ELSE -- another value, or formula
END AS Something
如果要将其应用于某些复杂公式的结果,则可以使用子查询:
SELECT CASE
WHEN Balance = 0 THEN 0
ELSE Something / Balance
END AS SomethingDividedByBalance
FROM (SELECT -- huge formula ending with AS Balance
, -- another formula AS Something
FROM yourTables
) subquery