Cumulative Sum's

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
Raamc
Premium Member
Premium Member
Posts: 87
Joined: Mon Aug 20, 2007 9:08 am

Cumulative Sum's

Post by Raamc »

Hi,

I am trying to calculate the cumulative sum of amounts for an account in my job.

I have a file with the sample data as below
ACCNO AMOUNT
================
123 12
123 13
123 03
234 5
234 5
234 6
In the target file i want o/p to be
ACCNO AMOUNT
====================
123 12
123 25
123 28
234 5
234 10
234 16

Please suggest me how to calculate these sums?
Thanks,
Raamc
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Two Aggregator stages.

Code: Select all

Detail ---->  Copy1  -----------------------------> Join2 ---->  Target
                |                                     ^
                |                                     |
                +--> Copy2  ----->  Aggr1 ------->  Join1
                        |                             ^
                        |                             |
                        +-------->  Aggr2  -----------+
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
ShaneMuir
Premium Member
Premium Member
Posts: 508
Joined: Tue Jun 15, 2004 5:00 am
Location: London

Post by ShaneMuir »

At a glace the only thing I can think of is to use a transform with stage variables that checks the key value and adds the values together, whilst the key value remains the same.

You would require 1 stage variable to hold the previous rows key value, one to hold/compare that to the current row's key value, one to add the values, and one to reset the total to zero if the key value changes. You then set your output derivation to be the value of the stage variable holding the cumulative total for that row.

Remember to hash partition and sort your input stream for this to work.
Raamc
Premium Member
Premium Member
Posts: 87
Joined: Mon Aug 20, 2007 9:08 am

Post by Raamc »

I designed my job with the Keychange column property in sort stage and staging variables in the transformer stage as below.
with the Key Change column property in the before sort stage i am capturing whether the key is changing or remaining same or not.
And in the staging variable in transformer, if Keychange=0 then Stagevar+Amount else pass the amount as it is.

Drag this staging variable amount field into CUMULATIVE AMOUNT COLUMN in the final o/p. :lol:
Thanks,
Raamc
Post Reply