Skip to content
Advertisement

Windowing functions cumulative aggregation

I am calculating cumulative by summing some columns. The code is working. But I want to include an extra variable for the first line only. Then it miscalculates.

What I want to do in line 1:

SUM(@devir + netTakas - ToplamCikis + YeniKrediKullanımı - Islem)

What I want to do except line 1:

SUM(netTakas - ToplamCikis + YeniKrediKullanımı - Islem)

When I do this with case, the wrong result is obtained.

SQL code:

declare @devir float = 1308973;
    WITH asilTablo AS(
        select * from #tmpbrut1
        union
        select * from #tmpbrut
    )
    SELECT 
        *,
        CASE WHEN ROW_NUMBER() OVER(order by takasTarihi) >= 2 then
        SUM(netTakas - ToplamCikis + YeniKrediKullanımı - Islem) OVER (ORDER BY takasTarihi ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
        else SUM(@devir + netTakas - ToplamCikis + YeniKrediKullanımı - Islem)  OVER (ORDER BY takasTarihi ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) end AS [Kümülatif Toplam]
    FROM asilTablo

So for example on line 1, it will do this:

@devir+netTakas-ToplamCikis+YeniKredKullanımı-Islem = Kümülatif Toplam

In places other than line 1, it will do this:

Kümülatif Toplam + netTakas-ToplamCikis+YeniKredKullanımı-Islem

enter image description here

@devir = 10 operation on the first line:

@devir + netTakas - ToplamCikis + YeniKrediKullanımı - Islem

enter image description here

Operations on the other line:

netTakas - ToplamCikis + YeniKrediKullanımı - Islem

Operations of variables and columns in the first row, The sum of the columns in the row 1 and the column in the “cumulative total” in the next row

CREATE TABLE #Tmp1(
     tarih date,
    netTakas int,
    ToplamCikis int,
    YeniKrediKullanımı int,
    Islem int
)

CREATE TABLE #Tmp2(
     tarih date,
    netTakas int,
    ToplamCikis int,
    YeniKrediKullanımı int,
    Islem int
)

insert into #Tmp1 (tarih, netTakas, ToplamCikis, YeniKrediKullanımı, Islem) VALUES ('20211227', 125, 34, 57, 347)
insert into #Tmp1 (tarih, netTakas, ToplamCikis, YeniKrediKullanımı, Islem) VALUES ('20211228', 135, 34, 587, 3624)
insert into #Tmp1 (tarih, netTakas, ToplamCikis, YeniKrediKullanımı, Islem) VALUES ('20211229', 125, 347, 577, 34)
insert into #Tmp1 (tarih, netTakas, ToplamCikis, YeniKrediKullanımı, Islem) VALUES ('20211230', 175, 344, 597, 374)
insert into #Tmp1 (tarih, netTakas, ToplamCikis, YeniKrediKullanımı, Islem) VALUES ('20211231', 1875, 364, 587, 38684)
insert into #Tmp1 (tarih, netTakas, ToplamCikis, YeniKrediKullanımı, Islem) VALUES ('20220103', 1295, 354, 575, 344)
insert into #Tmp1 (tarih, netTakas, ToplamCikis, YeniKrediKullanımı, Islem) VALUES ('20220104', 1285, 344, 547, 344)

insert into #Tmp2 (tarih, netTakas, ToplamCikis, YeniKrediKullanımı, Islem) VALUES ('20211226', 12453485, 3444, 5447, 34544)

declare @devir float = 1308973;
WITH asilTablo AS
(
    SELECT *
    FROM #Tmp1
union
Select *
from #Tmp2
)
SELECT  
    tarih,
    netTakas,
    ToplamCikis,
    YeniKrediKullanımı,
    Islem,
    CASE WHEN ROW_NUMBER() OVER(order by tarih) >= 2 then SUM(netTakas - ToplamCikis + YeniKrediKullanımı - Islem) OVER (ORDER BY tarih ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) 
    else  SUM(@devir + netTakas - ToplamCikis + YeniKrediKullanımı - Islem) OVER (ORDER BY tarih ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)  end  AS [Kümülatif Toplam]
FROM
    asilTablo

Advertisement

Answer

use ROW_NUMBER() on inner query. On outer query check for when ROW_NUMBER() = 1 include @devir in calculation

SELECT
    SUM (CASE WHEN RN = 1 THEN @devir ELSE 0 END
         + netTakas - ToplamCikis + YeniKrediKullanımı - Islem) 
    OVER (ORDER BY tarih)
FROM
(
    SELECT *, RN = ROW_NUMBER() OVER(order by tarih ROWS UNBOUNDED PRECEDING)
    FROM   asilTablo
) a
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement