Sql Oracle—跨行分布一行值
如附件所述。我需要将“B”列中的值分配为小于或等于A列。 现在对于A>B的情况2,我需要65的分布为44,16,5,但得到44,16,20Sql Oracle—跨行分布一行值,sql,oracle,oracle11g,aggregate-functions,Sql,Oracle,Oracle11g,Aggregate Functions,如附件所述。我需要将“B”列中的值分配为小于或等于A列。 现在对于A>B的情况2,我需要65的分布为44,16,5,但得到44,16,20 我就是这样理解这个问题的 SQL> with test (a, b) as 2 -- sample data 3 (select 0, 60 from dual union all 4 select 88, 0 from dual union all 5 select 32, 0 from dual union
我就是这样理解这个问题的
SQL> with test (a, b) as
2 -- sample data
3 (select 0, 60 from dual union all
4 select 88, 0 from dual union all
5 select 32, 0 from dual union all
6 select 7, 0 from dual
7 ),
8 -- query you need begins here
9 temp as
10 (select a,
11 max(b) over (order by null) b
12 from test
13 )
14 select
15 x.a,
16 case when x.a = 0 then y.b
17 else case when x.a > y.b then y.b
18 else x.b
19 end
20 end b
21 from test x left join temp y on x.a = y.a
22 order by x.a;
A B
---------- ----------
0 60
7 0
32 0
88 60
SQL>
我就是这样理解这个问题的
SQL> with test (a, b) as
2 -- sample data
3 (select 0, 60 from dual union all
4 select 88, 0 from dual union all
5 select 32, 0 from dual union all
6 select 7, 0 from dual
7 ),
8 -- query you need begins here
9 temp as
10 (select a,
11 max(b) over (order by null) b
12 from test
13 )
14 select
15 x.a,
16 case when x.a = 0 then y.b
17 else case when x.a > y.b then y.b
18 else x.b
19 end
20 end b
21 from test x left join temp y on x.a = y.a
22 order by x.a;
A B
---------- ----------
0 60
7 0
32 0
88 60
SQL>
SQL表表示无序集。但是你的结果假设了一个顺序。我将假设有一个排序列,并将其表示为
?
这是一个累加和问题——使用算术:
select t.*,
(case when b >= running_a then a
when b >= running_a - a then b - (running_a - a)
else 0
end) as new_a
from (select t.*, sum(a) over (order by ?) as running_a
from t
) t
是一个dbfiddle。SQL表表示无序集。但是你的结果假设了一个顺序。我将假设有一个排序列,并将其表示为?
这是一个累加和问题——使用算术:
select t.*,
(case when b >= running_a then a
when b >= running_a - a then b - (running_a - a)
else 0
end) as new_a
from (select t.*, sum(a) over (order by ?) as running_a
from t
) t
是一把D小提琴。谢谢,但是对于下面的大小写输入输出a>B a B 0 65 0 60 44 0 44 16 0 16 16 20 0 20 5,如果还有什么东西,我如何分配?目前输出是给我20。不客气,但是-这是不可读的。编辑原始问题并在其中发布数据。我认为SQL不能解决初始问题。只有当总和(A)等于B中的第1行时,它才起作用。也许,@are。正如我所说的,这就是我对它的理解。谢谢。前面的SQL是有效的,我后来编辑了这个问题。谢谢,但是对于下面的case IN OUT A>B A B A B 0 65 0 60 44 0 44 16 0 16 20 0 5我如何分配是否还有剩余的内容?目前输出是给我20。不客气,但是-这是不可读的。编辑原始问题并在其中发布数据。我认为SQL不能解决初始问题。只有当总和(A)等于B中的第1行时,它才起作用。也许,@are。正如我所说,这就是我对它的理解。谢谢。前面的SQL是有效的,我在后面编辑了这个问题。你的结果假设表上有一个顺序。SQL表表示无序集。您需要一列来指定排序。您的结果假定表上有一个排序。SQL表表示无序集。您需要一列来指定顺序。