Search code examples
pentahopentaho-spoon

pentaho spoon : how to insert value to column conditionnally?


So in my table, I have a column quantity and comment. If the value in quantity is more than 0, then I need to insert a string "available" to column comment , if it equals to 0 then "to order" and finally if it's less than zero, then "warning". What could be the best way?

edited: Guess my question above doesn't show the whole work necessary.

At first, I have a text file where I get fields including quantity.

Then I do some modifications of data (on formula step, I do some calculations on quantity).

In the end I use Table output step to insert them into BD. One of the fields to insert is quantity.

My main question is :

Is it better to insert values to column comment after Table output step (when quantity is already added in BD) using SQL script step?


Solution

  • ** answer no longer applies with new question details **

    If you are updating a database table, by far the best and most efficient solution is to do it in a single SQL statement.

    In a Pentaho Job, add a SQL step (under scripting).

    In that step enter the SQL command. It will be similar to:

    UPDATE MyTable
    SET comment = 
        CASE 
            WHEN quantity > 0 THEN 'available'
            WHEN quantity < 0 THEN 'warning'
            ELSE 'to order'
        END
    // next line optional, use it if you only need to update some of the records.
    WHERE (insert conditions here if you need any)
    

    As an extra comment, it's less than ideal to have two columns that should always be in sync, but depend on an external job to keep them in sync. There are techniques like database triggers or calculating the case/when while retrieving the rows in a select statement that eliminate the chance of having out of sync fields.