Hitachi Vantara Pentaho Community Forums
Results 1 to 12 of 12

Thread: setting variable to have max value of two other variables

  1. #1

    Default setting variable to have max value of two other variables

    I am able to get the max id from a table and save it in a variable using the set variables step.

    What i would like to do now is get the max from two tables and set a variable to be the max of these two values. I can set two variables, but i can't seem to find how i can perform a simple if on these two variables to set the value of a third variable

  2. #2
    Join Date
    May 2006
    Posts
    4,882

    Default

    Javascript springs to mind

    Regards,
    Sven

  3. #3

    Default

    Quote Originally Posted by sboden View Post
    Javascript springs to mind

    Regards,
    Sven
    yes, i saw that scripting box alright.

    But, how do i access kettle variables within the java script step?

    I tried the following:
    if(${maxClosedID}>${maxNotClosedID}){
    ${maxID}=${maxClosedID};
    }
    else{
    ${maxID}=${maxNotClosedID};
    }

    but it doesn't seem to work. i just want this calculation to be performed when i call the tranfromation from the job, not to be depenant on any rows coming into it.
    Last edited by theirishmole; 07-02-2009 at 07:12 AM.

  4. #4
    Join Date
    Nov 1999
    Posts
    9,729

    Default

    Use "Get Variables" to grab the values of the 2.
    If they are both Integers, you compare maxClosedID and maxNotClosedID with JavaScript:

    var maxID = maxClosedID > maxNotClosedId ? maxClosedID : maxNotClosedID

    The right side of the assignment can be used in "User Defined Java Expression" without change.

    Matt

  5. #5
    Join Date
    May 2006
    Posts
    4,882

    Default

    There are functions available to "get variables" and to "set variables" in the javascript step... open up the function tree on the left hand side of the step dialog ... transform functions/special functions... right click on getVariable and press sample.

    Regards,
    Sven

  6. #6

    Default

    in the java script step i have treid the following:
    var maxClosedID = getVariable("maxClosedID", 0);
    var maxNotClosedID = getVariable("maxNotClosedID", 0);
    var maxID = maxClosedID > maxNotClosedID ? maxClosedID : maxNotClosedID
    setVariable("maxID",maxID, "r");

    maxID is not getting set. is this the correct usage of get and set? it wasn't very clear from the samples. I have programmed before in java, but not in java script.

  7. #7
    Join Date
    May 2006
    Posts
    4,882

    Default

    When you set a variable you can't use that variable in the current transformation. Is this what you mean?

    Suppose you set a variable in a transformation, you probably can't use in the same transformation (there are some exceptions, but they're minor). But you will see the value in another transformation executed from the same job.

    Regards,
    Sven

  8. #8

    Default

    No, i am printing out the values of the three variables in the job, after the javascript step.

    The closed and not closed variables are being set but the max id is not.

    i have the javascript step on its own in a transformations, so there is no stream going into it. Will it still run? thats what i was wondering about before.

  9. #9
    Join Date
    May 2006
    Posts
    4,882

    Default

    Quote Originally Posted by theirishmole View Post
    i have the javascript step on its own in a transformations, so there is no stream going into it. Will it still run? thats what i was wondering about before.
    Funny ... it won't run... the javascript step runs on input... so put a row generator in front of it generating 1 empty row.

    Regards,
    Sven

  10. #10

    Default

    Quote Originally Posted by sboden View Post
    Funny ... it won't run... the javascript step runs on input... so put a row generator in front of it generating 1 empty row.

    Regards,
    Sven
    working now, i did say in my previous posts that i didn't want something which depended on row input

  11. #11
    Join Date
    May 2006
    Posts
    4,882

    Default

    An empty row in not real "row input" ... in this case it's a workaround to make javascript run (and it is has no side-effects).

  12. #12

    Default

    Quote Originally Posted by sboden View Post
    An empty row in not real "row input" ... in this case it's a workaround to make javascript run (and it is has no side-effects).
    Cool, i figured that was the case.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2019 Hitachi Vantara Corporation. All Rights Reserved.