Pentaho get variables from table input to use it in sql script - variables

I'm trying to get variables from table input, table input return this data:
ID, name
ID, name
.., ...
ID, name
I want to use this data to get other data in a other table input:
select id, name, date, nightmare from table where id = ${ID} and name = ${name}
or better:
select id, name, date, nightmare from table where id in ${ID} and name in ${name}
Note:
- ${..} -> variable
- I can't do subquery, each query are in different dbms
Thanks

What you're after aren't variables, which would need to be set before the transformation initialises and are always single values.
You're after parameters.
Your Table input1 returns several rows of data with fields id and name; you connect it to a new Table Input where you want to pass these values, for example, in the where clause:
select * from customers
where id = ? and name = ?
The question marks will be replaced by the values coming from the previous step.
You just need to fill in the name of the first step in the box "Insert data from Step" and check the "Execute for each row" box.
Now your query will run once for each row arriving at that step, and the values will be replaced.
Caveat:
each parameter is used only once;
and they will be passed in the order in which they arrive;
all incoming fields must be used in the query.
If you need to use the same parameter multiple times or use them in a different order you'll need to change the structure of your incoming data stream with a Select Values step.

Related

Handling Json data in snowflake

enter image description here
I have a table which contains Json file data in each row which gets updated into my snowflake table every weak. I am extracting values from the Json files into another table. When the data is loaded in Json format there are multiple entries of the same ID. So, when I extract values from Json to a table there are duplicate rows. How do I tackle them in order to get the distinct rows only. My select query look something like this:
select
json_data:data[0].attributes."Additional Invoice?":: string as "Additional Invoice?",
json_data:data[0].attributes."Additional PO?":: string as "Additional PO?",
json_data:data[0].attributes."Aggregate Contract Value":: number as "Aggreagate Contract Value" ,
json_data:data[0].attributes."Annualized Baseline Spend" :: number as "Annualized Baseline Spend",
json_data:data[0].id ::number as ID,
json_data:data[0].type::string as TYPE
from scout_projects order by ID
the scout project file screenshot is attached.
The attached Screenshot is the output form the given query and as you could see the ID column is the same but there are only 2 unique rows. I want my query to return only those 2 unique rows.
select distinct json_data:data[0].id :: number as ID from scout_projects
what is the approach should I take?
I tried using subquery, but it gave me error stating "single-row subquery returns more than one row. snowflake error" which is obvious. so, need a way out .

How to create Selection and connecting output of deliveries and positions of delivery

How can I create a selection criteria and get the output fields?
Example:
I need the selection to be delivery number and the output field would be delivery number, material number, quantity etc.
How can I write the select statement to get the result from the fields ?
First of all, a PARAMETER is necessary (or SELECT-OPTIONS, depending on what you need):
PARAMETERS: p_delnr TYPE <delivery_number>.
Then make a select statement:
SELECT <delivery_number> <material_number> <quantity>
FROM <DATABASE TABLE>
INTO CORRESPONDING FIELDS OF <local_structure> "(or local table, with the TABLE addition)
WHERE <delivery_number> = p_delnr.
Then, after you have the data selected, you can output it with a WRITE statement or any way you want.
You should also check out following documentations, for more information:
SELECT STATEMENT
PARAMETERS
SELECT-OPTIONS

Can you concatenate two strings to create dynamic column names in PostgreSQL?

I have a form where people can type in a start and end date, as well as a column name prefix.
In the backend, I want to do something along the lines of
SELECT *, CAST('{{startDate}}' AS TIMESTAMP) AS ({{prefix}} + '_startDate')
Is this possible? Basically, I want to dynamically create the name of the new column. The table is immediately returned to the user, so I don't want to mutate the underlying table itself. Thanks!
You can execute dynamic query that you have prepared by using EXECUTE keyword, otherwise it is not possible to have dynamic structure of SQL.
Since you are preparing your SQL outside database, you can use something like:
SELECT *, CAST('{{startDate}}' AS TIMESTAMP) AS {{prefix}}_startDate
Assuming that {{prefix}} is replaced with some string by your template before it is sent to database.

SSRS: When Multi-value Parameter = Select ALL remove filter in Script

I have a report that has multiple multi-value parameter. What I wanted to do is if the parameter is = Select All I'll remove that parameter to my SQL Script.
Example is I have a Product group and Product Name parameter and what I want is if the user selects all the product group my script will be like:
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
While if the user did not select all Product Group, my script will be like:
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
AND PRODUCT_GROUP IN (#ProductGroup)
I want to know how Can I detect when multi-value parameter is = Select All. I think it will really help the loading time of the tool if I just remove the filter on my script.
As far as I know, you cannot detect when "Select All" has been checked by the user.
To remove the filter completely when the user selects all the choices in a multi-valued parameter you would have to employ logic in your stored procedure that checks to see if all the choices were passed to it in the parameter, and if so, don't use the parameter at all in the main query.
You can add your own ALL value to the parameter. In the dataset query check if the user have selected 'ALL' if so don't use the parameter.
Something like this:
IF ('ALL' IN #ProductGroup)
BEGIN
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
END
ELSE
SELECT * FROM TABLE WHERE PRODUCT_NAME IN (#ProductName)
AND PRODUCT_GROUP IN (#ProductGroup)
It is not tested but should work
If your parameter is based on a dataset, you can compare the numbers of elements selected in the parameter vs. the number of items in the dataset for the parameter.
=IIF(Parameters!AREA.Count = CountRows("Areas"), "ALL", "Some")

Extracting a string value from a pipe-separated string in a table column

I'm an Oracle newbie.
I have a column in my table that contains a pipe(|)-separated string such as this:
uguyguyguyguyguy|0737494110|noreply#xxyyxx.se|E:\PROD_ActionMarketer\Program\Apsis\ApsisREST.exe|E:\PROD_ActionMarketer\Temp\TempApsisAdmin.skv|SENDEMAIL|"LIST=Daily SMS SPL"|1015832
As you see the penultimate value begins with "LIST=". I want to extract the value after this string i.e. "Daily SMS SPL" to be able to compare it with another column in another table. I want to obtain this value ("Daily SMS SPL") using a SELECT query if it's possible.
So let's say the table name is MYTABLE and the name of the column is MYCOLUMN.
SELECT SUBSTR(surname,1
,INSTR(surname,'"',1,1)-1) FROM
(SELECT SUBSTR(column
,INSTR(column,'LIST',-1)+5
) AS surname
FROM table)
FIDDLE