Calculating count ONLY on first occurence of each customer

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
gateleys
Premium Member
Premium Member
Posts: 992
Joined: Mon Aug 08, 2005 5:08 pm
Location: USA

Calculating count ONLY on first occurence of each customer

Post by gateleys »

My input records are as follows, with multiple records for each customer.

Code: Select all

Cust_ID  Cust_Name  Value
1         AA             100
2         BB             200
1         AA             50
Now, for the first occurence of each Cust_ID, I need to multiply Value by 5. However, for subsequent occurence of each customer's ID, I just pass the values as they are. So, my result looks like -

Code: Select all

Cust_ID  Cust_Name  Value
1         AA             500    (after multiplication by 5)
2         BB             1000  (after multiplication by 5)
1         AA             50      (remains as it is)
Please elaborate how I can do this!!
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Hello Gateleys,

the simplest and most efficient method would be to sort the incoming stream by Cust_ID and then to use a stage variable to store the last Cust_ID value and to do a comparison on this value with the current Cust_ID to see if this has changed in order to do your x5 computation.
PhilHibbs
Premium Member
Premium Member
Posts: 1044
Joined: Wed Sep 29, 2004 3:30 am
Location: Nottingham, UK
Contact:

Post by PhilHibbs »

ArndW wrote:Hello Gateleys,

the simplest and most efficient method would be to sort the incoming stream by Cust_ID and then to use a stage variable to store the last Cust_ID value and to do a comparison on this value with the current Cust_ID to see if this has changed in order to do your x5 computation.
I do this a lot. It relies on the fact that stage variables are evaluated in the order that they appear, so if you use the value of one stage variable in the derivation of another stage variable that is above it, then it gets the value from the previous record.

Code: Select all

svKey        | link.Key
svKeyChanged | svKey <> svOldKey
svOldKey     | svKey
I can't remember whether or not svKey is null when the first record is processed. I have seen multiple NULL keys treated as separate key values, so you might want to code around that.
Phil Hibbs | Capgemini
Technical Consultant
I_Server_Whale
Premium Member
Premium Member
Posts: 1255
Joined: Wed Feb 02, 2005 11:54 am
Location: United States of America

Post by I_Server_Whale »

There is no necessary to code around that because Cust_Id will never be NULL. I think when the first record is processed, the stage variable (svKey) doesn't hold any value (probably NULL).

So, when you process the first record, always the OLDSVKEY<>SVKEY.

I agree with Arnd, this is the best way to do it!

Naveen.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

naveendronavalli wrote:I think when the first record is processed, the stage variable (svKey) doesn't hold any value (probably NULL).
Don't "think" - know what it is by explicitly setting it's Initial Value in the stage variables property box. :wink:

And set it to something non-null that you know will be different than any first value coming in so the first 'not equal' check will be true. Keep in mind the fact that - if the initial value of the stage variable is null - that first not equal check will take the false path.
-craig

"You can never have too many knives" -- Logan Nine Fingers
gateleys
Premium Member
Premium Member
Posts: 992
Joined: Mon Aug 08, 2005 5:08 pm
Location: USA

Post by gateleys »

THankx Arnd, I got it right.
ArndW wrote:Hello Gateleys,

the simplest and most efficient method would be to sort the incoming stream by Cust_ID and then to use a stage variable to store the last Cust_ID value and to do a comparison on this value with the current Cust_ID to see if this has changed in order to do your x5 computation.
Post Reply