check if condition is met then do a lookup - sql

I'm trying to do a parallel job wherein I check the value of a column where in if it matches a certain condition it returns a certain value and if does not it will check on another condition and if it matches that condition it will do a lookup.
I'm not sure where to put the condition if it will be on the reference link or the output link.
Here is a pseudocode of some sort to hopefully makes is clearer:
if (table1.colname = NULL OR table1.colname = '')
then '999'
else
if table1.column = 0
then do a lookup on table2.colname for '1' return table2.colname2
else
do a lookup on table2.colname for '2' return table2.colname2
if value is not found then '999'
I'm kinda new to datastage, so any comments or idea are greatly appreciated.
And please do let me know if you need other clarifications.
Thank you.

Use Transformer or Filter to split the records into three links based on the condition
First Link : Records containing Null or Empty
Second Link : Records containing Value Zero
third Link : Otherwise ( Remaining records)
Then do the look up on second and third link seperately as per the requirement
Then Funnel the output from First link and the outputs from Look up
Use transformer to apply the fourth condition for non matching records
Or
In transformer create a new field in which write condition like
If NullToEmpty(table1.column)='' then "DefaultValue"(Make sure that the value u are assigning should not have the matching records in reference table)
Else If table1.column = 0
then "1"
Else If table1.column = 1
then "2"
Else "Defaultvalue"
Then do the lookup on this new field with the table2.colname2 and assign "999" in the transformer for the lookup not found records

Related

Tableau: Get the ids that contain only the selected values from another column

I have the following question!
I have a table like this:
Data Source
I want to create a field(i suppose it's a field) that i can take the apl_ids,
that have as service_offered some that i want.
Example from the above table. If i want the apl_ids that have ONLY the service_offered
Pending 1, Pending 2 and Pending 7.
In that case, I want to get the apl_id = "13" since apl_id = "12" got one more service that i don't need.
Which is the best way to get that?
Thank you in advance!
Add a calculated field which gives 1 for desired values and 0 for other values. Add another calc field with fixed LOD to apl_id to sum of calcF1. Filter all ids with values=3 only. I think that should work.
Else tell me I will post screenshots
You can create a set based on the field api_id defined by the condition
max([service_offering]=“Pending 1”) and
max([service_offering]=“Pending 2”) and
max([service_offering]=“Pending 7”) and
min([service_offering]=“Pending 1” or [service_offering]=“Pending 2” or [service_offering]=“Pending 7”)
This set will contain those api_ids that have at least one record where service_offering is “Pending 1” and at least one record with Pending 2 ... and where every record has a service offering of 1, 2 or 7 (I.e. no others)
The key is to realize that Tableau treats True as greater than False, so min() and max() for boolean expressions correspond to every() and any().
Once you have a set of api_ids() you can use it on shelves and in calculated fields in many different ways.

Displaying different table values depending on the value of another table/column

I'm quite new to SQL and generally can find my way with basic stuff, but this time I'm kind of stuck...
I'm trying to retrieve (and display) the different configurations of an online app that are stored in a MSSQL DB.
I have to look at the value of a certain table and column (let's call it "configName.id"), and depending on the value returned (negative, positive, NULL), I have to match it in different tables and display one of 3 options in the same column.
I tried using CASE, but I don't want to display 3 different columns; I want to query different tables, and display the results in a single column. The displayed column name should be "Configuration Name".
In basic algorithm language, this would be:
if (configName.id > 0)
-> Display the corresponding value from table "a"
if (configName.id < 0)
-> Invert the value returned to be positive, display the corresponding value from table "b"
else
-> Display "NULL" as results.
My challenge is that regardless of the results returned from any table, I must display it under my report column "Configuration Name".
Any help would be really appreciated, thanks!
It would be something like this
select coalesce(a.value, b.value) as value
from configname left join
a
on a.id = configname.id and configname.id > 0 left join
b
on b.id = abs(configname.id) and configname.id < 0;

Showing specific data without filtering out query data

I need to build a form where one field (Unplanned Amount) will only populate with data if another field (status) equals a certain value ("not in workflow"). If the status equals anything else, Unplanned amount field would be blank.
The data is coming from three different tables:
Table 1) AccountNum
Table 2) DocNum, DocAmount, DocStatus
Table 3) CommitAmount
The value in CommitAmount will always equal DocAmount, but the value of DocAmount doesn't have to equal the value of the CommitAmount if it's "unplanned."
I tried putting the data into a query and using the following code on my form to no avail:
If Me.DocStatus = "Not in workflow" Then
Me.DocAmount = Null
Else
Me.DocAmount = [forms]![form2]![DocAmount]
End If
Does anyone know how to go about making a query-based form or report that allows what I've described above to take place? Or maybe this should not be done via a query?
Thanks!!
Put the IF statement into to data source for me.docamount
OR use a case statement in the query itself
select case docstatus when 'Not in workflow' then null else docamount end

Separate column based on condition in same table in SQL

I'm having a result set from a table in which I have to split a specific value and display in another column(user defined) based on a condition.
//Query
SELECT wtcCommentId, wtcTransactionId, wtcTaskId, wtcUserLogin,
wtcCommentDateTime, wtcCommentText
FROM dbo.tblWorkflowTransactionComments (NOLOCK)
WHERE wtcTransactionId = 141248
The output for the above query will be some 10 rows.
In the above query I need to separate the wtcCommentText column if the wtcTaskId for that result is 0. So if the wtcTaskId is 0 the wtcCommenttext value should be displayed in a separate column(transcommenttext) and for others it should be displaying in the same column(wtcCommentText). So that I can make a split between the comments based on the taskId.
How can I achieve this?
I'm not sure it really makes sense, but okay, I'm game for a laugh:
SELECT wtcCommentId, wtcTransactionId, wtcTaskId, wtcUserLogin,
wtcCommentDateTime,
CASE WHEN wtcTaskId!=0 THEN wtcCommentText END as wtcCommentText,
CASE WHEN wtcTaskId=0 THEN wtcCommentText END as transcommenttext
FROM dbo.tblWorkflowTransactionComments (NOLOCK)
WHERE wtcTransactionId = 141248
It seems like the kind of trivial reformatting that would be better done in a front end or report generator though.

increment SQL field manually

I'm attempting to create a SAP UDO for product registrations. The UDO automatically creates two required fields "docentry" and "code." docentry auto-increments, code does not. I don't have accessing to the underlying database structure to fix that, but I can attach a query to the code field.
When a record is created, can I setup a query that would increment based on the previous row's value for code? I'm not looking for anything useful out of this field, but we will need to both batch import .csv files on a regular basis and also take direct submissions from our site, so I'm just looking for the easiest way to make it work.
Thank you.
UPDATE table_name_here SET code = code + 1 WHERE ? = ??
Replace the ? with unique identifier column name and ?? with it's value for row you are working with.
After your comment:
Let's say you have previous row code value stored in $1 and actual row id in $2. Then:
UPDATE registrations SET code = $1 + 1 WHERE id = $2
After your second comment:
UPDATE registrations SET code = (SELECT MAX(code) FROM registrations) + 1 WHERE docentry = `new_row_docentry_value`