1

Topic: Help with request sql

There are values
1 408.96
2-414.63
3 563.94
4-308.73
5-246.35
It is necessary to me
step
408.96-414.63
1 408.96 0
2-414.63-5.67
step
408.96-414.63+563.94
1 408.96 0
2-414.63 0
3 563.94 558.27
step
408.96-414.63+563.94-308.73
1 408.96 0
2-414.63 0
3 563.94 249.54
4-308.73 0
step
408.96-414.63+563.94-308.73-246.35
1 408.96 0
2-414.63 0
3 563.94 3.19
4-308.73 0
5-246.35 0
Those I want to learn for what period there was the positive residual
It is desirable for one select

2

Re: Help with request sql

Hello, anything it is not clear

3

Re: Help with request sql

Perederiy;

select * from b
"n" "s"
"1" "408,96"
"3" "563,94"
"4" "-308,73"
"5" "-246,35"
"2" "-414,63"
select b.n, b.s, b1.s, b.s + b1.s as ost
from b
left join b b1 on b.n=b1.n-1
where b.s + b1.s> 0
order by b.n;
Output
"n" "s" "s_1" "ost"
"2" "-414,63" "563,94" "149,31"
"3" "563,94" "-308,73" "255,21"

4

Re: Help with request sql

select b1.n, (select sum (s) from b where b.n <=b1.n)
from b b1
order by b1.n

And  the job is not clear.

5

Re: Help with request sql

It is necessary to carry out the debt receivable analysis
Number of the period a debt (or an overpayment) for the period
I do
select id;
case
when summ - sum (summ)
over (order by id ROWS BETWEEN UNBOUNDED PRECEDING and CURRENT ROW) <= 0 then
0
else
sum (summ) over (order by id ROWS BETWEEN UNBOUNDED PRECEDING and UNBOUNDED FOLLOWING)
end result
from tmp_t
For a variant
"1"; 191.88
"2";-442.82
"3"; 675.57
"4";-332.76
"5";-34.79
Works
"1"; 0
"2"; 0
"3"; 57.08
"4"; 0
"5"; 0
Those the program correctly defined the general debts as debts for 3 period
But for
Variant
"1"; 1144.73
"2"; 4503.82
"3"; 8260.20
"4"; 799.43
"5"; 7914.11
Does not work
"1"; 0
"2"; 0
"3"; 0
"4"; 0
"5"; 0
Though as minuses were not - it should transfer the positive values simply
Those
"1"; 1144.73
"2"; 4503.82
"3"; 8260.20
"4"; 799.43
"5"; 7914.11
Help with request

6

Re: Help with request sql

Perederiy;
Misters if wish the help:
- Give a script which generates tables with the initial data
- Show request which gave birth also to its output
- Show a desirable output.