在SQL中,你幾乎可以用這一套left join
就做這個。
select (t1.col + coalesce(t2.col, 0) + coalesce(t3.col, 0) +
coalesce(t4.col, 0) + coalesce(t5.col, 0)
) as sumcombo
from t t1 left join
t t2
on t1.col < t2.col left join
t t3
on t2.col < t3.col left join
t t4
on t3.col < t4.col left join
t t5
on t4.col < t5.col;
它不相當的工作,因爲你永遠無法得到的只是「A」的實例,而不是:。
with t as (
select col
from table
union all
select NULL
from dual
)
select (t1.col + coalesce(t2.col, 0) + coalesce(t3.col, 0) +
coalesce(t4.col, 0) + coalesce(t5.col, 0)
) as sumcombo
from table t1 left join
t t2
on t1.col < t2.col or t2.col is null left join
t t3
on t2.col < t3.col or t3.col is null left join
t t4
on t3.col < t4.col or t4.col is null left join
t t5
on t4.col < t5.col or t5.col is null;
你只是試圖做的可能的組合計數?這是一個衆所周知的公式,不需要任何SQL。或者你是否試圖實際列舉每種可能的組合? – Rabbit
你好,不,我不是隻想做一些可能的組合。我需要每個組合的結果 –
爲什麼你需要這樣的東西?您的服務器將無法處理它。該表幾乎不可用 – Rabbit