我正在看一些oracle代碼,發現這些case陳述句是用 運算子連接的? 它在這里做什么?是否有可能避免使用 " "來做同樣的事情?
case
when t1.id is not null
then 1
else
0
end
case
when t2.id is not null
then 2
else
0
end
case
when t3.id is not null
then 4
else
0
end filter。
uj5u.com熱心網友回復:
雖然你的案例代表了普通的加法運算,但另一種方法是使用NVL2()函式,它將空和非空情況作為條件,使其更簡短,如
SELECT NVL2(t1. id,1,0) NVL2(t2. id,2,0) NVL2(t3. id,4,0) AS 過濾器...
uj5u.com熱心網友回復:
或者,還有一個選擇,DECODE:
select
decode(t1.id, null, 0, 1)
decode(t2.id, null, 0, 2)
decode(t3.id, null, 0, 4) asfilter
from ...。
uj5u.com熱心網友回復:
我沒有看到任何明顯的方法使邏輯更簡潔,但這里有一個解釋。
case when T1. id 是 不是 null then 1 else 0 end -- return 1 or 0
case when t2. id is not null then 2 else 0 end -- return 2 or 0
case when t3. id 是 不是 null 那么 4 else 0 end --回傳4或0。
上面的每個CASE運算式都回傳一個整數值,如果特定的id不是NULL,則是1、2或4,否則就是0。 運算子是簡單地將這些整數運算式相加。
uj5u.com熱心網友回復:
是否有可能避免' '而做同樣的事情?
事實上,是的。 假設ids都有相同的型別,你可以使用一個橫向連接:
from . . --你現有的東西都在這里。
cross join lateral
(select count(id) as cnt
from (select t1.id from dual union all)
t2.id from dual union all
t3.id from dual
) x
) x
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/334031.html
標籤:
