我有这张桌子。只有明细表获得价值;我想得到分支的总和。
ACCOUNT Value
---------- -------------
100
100-01
100-01-01
100-01-01-001 7
100-01-01-006 6
100-01-01-271 5
100-02
100-02-01-001 1
100-02-01-006 2
100-02-01-271 3
Run Code Online (Sandbox Code Playgroud)
喜欢
ACCOUNT Value
---------- -------------
100 24 -- (sum of 100%)
100-01 18 -- (sum of 100-01%)
100-01-01 18 --(sum of 100-01-01%)
100-01-01-001 7
100-01-01-006 6
100-01-01-271 5
100-02 6 -- (sum of 100-02%)
100-02-01-001 1
100-02-01-006 2
100-02-01-271 3
Run Code Online (Sandbox Code Playgroud)
我尝试通过 and 进行 sum 并尝试 sum(value) where account like account||'-%' 但不能结束
使用子查询:
select account, (select sum(value) from t where account like a.account||'%') account from t a
Run Code Online (Sandbox Code Playgroud)
例子:
with t(account, value) as (
select '100', null from dual union all
select '100-01', null from dual union all
select '100-01-01', null from dual union all
select '100-01-01-001', 7 from dual union all
select '100-01-01-006', 6 from dual union all
select '100-01-01-271', 5 from dual union all
select '100-02', null from dual union all
select '100-02-01-001', 1 from dual union all
select '100-02-01-006', 2 from dual union all
select '100-02-01-271', 3 from dual )
select account, (select sum(value) from t
where account like a.account||'%') account from t a
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48 次 |
| 最近记录: |