我的要求可以通过以下MySQL MySQL DB中的SQL进行降级:
CREATE TABLE `test` (
`tran_date` datetime DEFAULT NULL,
`qty1` int(11) DEFAULT '0',
`qty2` int(11) DEFAULT '0'
) ;
insert into test values('2019-01-01 10:00:00',1,0);
insert into test values('2019-01-01 10:00:01',2,0);
insert into test values('2019-01-01 10:00:03',0,1);
insert into test values('2019-01-01 10:00:04',0,2);
insert into test values('2019-01-01 10:00:05',1,0);
insert into test values('2019-01-01 10:00:05',3,0);
insert into test values('2019-01-01 10:00:06',0,4);
insert into test values('2019-01-01 10:00:07',0,1);
select tran_date, qty1, qty2, case when qty1=0 then (@total := @total + qty2 ) when qty1<=@total then (@total := @total - qty1)
else ( @total := 0 ) end as qty3
from test , (select @total := 0 ) as T1
order by tran_date;
+---------------------+------+------+------+
| tran_date | qty1 | qty2 | qty3 |
+---------------------+------+------+------+
| 2019-01-01 10:00:00 | 1 | 0 | 0 |
| 2019-01-01 10:00:01 | 2 | 0 | 0 |
| 2019-01-01 10:00:03 | 0 | 1 | 1 |
| 2019-01-01 10:00:04 | 0 | 2 | 3 |
| 2019-01-01 10:00:05 | 1 | 0 | 2 |
| 2019-01-01 10:00:05 | 3 | 0 | 0 |
| 2019-01-01 10:00:06 | 0 | 4 | 4 |
| 2019-01-01 10:00:07 | 0 | 1 | 5 |
+---------------------+------+------+------+
我想要的是qty3列,应该根据上一行中的值以及同一行中qty1和qty2的值进行计算。对于第一行,其值始终为0。 规则是:
1. if qty1=0, the qty3 = ${qty3_in_previous_row_value} + qty2
2. if qty1 <= ${qty3_in_previous_row_value}, the qty3 = ${qty3_in_previous_row_value} - qty1
3. else, qty3 = 0
以上SQL可以在MySQL DB中工作,我想在Oracle DB中找到一个执行相同功能的SQL。 非常感谢您的帮助!
答案 0 :(得分:0)
您可以对类似于MySQL解决方案的问题使用递归查询:
ng-template