在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