When the summary reaches the limit of 8k or above it resets to left import, the first column its the import, te second the summary, the third how we want to be resets when >= 8000 and then continue the summary from that point
please provide usable sample data in the following format
create table #galion(import decimal(6,2), summary decimal(6,2))
insert into #galion
select 2085.06, 2085.06 union
select 5078.48, 7163.54
and do this for all of the sample rows you provided a picture of. Help us help you.
That way we can run and test your data on our local sql server and try to provide you the correct answer. We cant do that with images and describing things with first column, second column and third etc
I suspect the 'Quirky Update' will be the quickest way to do this in tsql. You can google this.
The technique is non-relational and relies on tsql specific syntax.
What I'm trying is to sum an import in another column, but when it reaches >=8000 resets and continues from there, I'm trying my best to express my idea not good in english sorry for that.
This query is wrong becouse the next time the summatory will be always > 8000 so the group does not change, is there a way to reset sum() to the last import value?
create table #a (id int identity(1,1),
StartDate date,
RFC varchar(30),
Amount decimal(10,2))
insert into #a values
('1/1/2020', 'abc', 1050),
('1/4/2020', 'abc', 2625),
('1/6/2020', 'abc', 1389.50),
('1/7/2020', 'abc', 525),
('1/8/2020', 'abc', 2625),
('1/9/2020', 'abc', 4516.24),
('1/10/2020', 'abc', 6650),
('1/11/2020', 'abc', 5550)
select v.*,
sum(Amount) over (partition by grp order by StartDate) as CumulativeAmount
from (
Select *,
sum(Amount) over (partition by RFC order by StartDate) as CumulativeAmount,
cast(sum(Amount) over (partition by RFC order by StartDate) / 8000 as int) as grp
from #a) v
UPDATE @xx
SET @temp = CumulativeAmount = case when COALESCE(@running_sum, 0) + Amount >= 8000 then Amount else COALESCE(@running_sum, 0) + Amount end
, @running_sum = CASE WHEN @temp < 8000 THEN @temp ELSE 0 END
OPTION (FORCE ORDER)