Showing specific data without filtering out query data - sql

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

Related

Why does a CASE WHEN give me a different result to a WHERE with the same condition?

I'm trying to use a case when and a pivot to filter some data, but get a result of a total count of 0, however, when I use the same condition in a where statement I get a result that's more what i expected.
SELECT * FROM(SELECT FYEAR,
CASE WHEN (DIAG_3_01 IN ('E10','E11','E12','E13','E14','O24') OR DIAG_4_01 IN ('E232','N251','P702')) AND (OPERTN_3_01 IN ('N26','P22','X07','X09','X10','X11') OR OPERTN_4_01 IN ('Q011','X215','X216','X273','X121')) THEN 'a'
ELSE 'Other' END AS 'Procs',
(FCE)
FROM database
) AS a
PIVOT(COUNT(FCE) FOR [Procs] IN ([a])) AS p;
So this results in a table with column name a and a row value of 0, whereas this code results in a total of about 4000:
SELECT COUNT(FCE)
FROM database
WHERE (DIAG_3_01 IN ('E10','E11','E12','E13','E14','O24') OR DIAG_4_01 IN ('E232','N251','P702')) AND (OPERTN_3_01 IN ('N26','P22','X07','X09','X10','X11') OR OPERTN_4_01 IN ('Q011','X215','X216','X273','X121'));
Unfortunately I cant share the database contents, but would appreciate any insight as to why this might be happening.
The CASE statement is like an If-statement, which only returns the very first value it founds that meets the criteria. You can also check multiple cases with WHEN and if no criteria are met, then the query will show whatever is in the ELSE statement.
The WHERE clause is just filtering the result, meaning it tell the query which records should not be ignored.

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;

SQL Server Report Builder - How to grey out a parameter with multiple sub-reports?

I have a main report which has three sub-reports. I am trying to grey out one of the parameter option when a certain report is selected. The reason I want to do that is because one of the sub report does not use this parameter.
Here's the code I am using now. The parameter displaying the query result is #device.
--When user select report 2, the parameter displays the device list from table2.
IF #selectReport = 2
BEGIN
SELECT DISTINCT type
FROM table2
END
--When user select report 3, the parameter displays the device list from table3.
IF #selectReport = 3
BEGIN
SELECT DISTINCT type
FROM table3
END
--When user select report 1. I want to grey out the parameter, but I could not do it.
--So I created the table contains NULL value.
--So, when the user select the report 1, the parameter will show only null value.
IF #selectReport = 1
BEGIN
SELECT DISTINCT type
FROM nullValueTable1
END
I want it to be grey out when the report 1 is selected instead of show NULL on drop down list. Any idea???
You can't grey out the parameter. Unfortunately you can't hide the parameter either as the hidden property only takes True and False. What you are doing now might be the best you can do. However, you could try looking at cascading parameters, maybe you might be able to provide a slightly more user friendly value like "None" to the dropdown.

How to write single query instead of using loop in oracle?

I would like to write a query based on some conditions as shown below. Could you please help me out in this regard?
RecordID MergeRecordID Status StatusTime
3700432 289015 Invoiced 25-May-09
3700433 289015 Invoiced 25-May-09
3700434 289015 Invoiced 08-Dec-09
3700435 289015 Error 08-Dec-09
I have a table called billingValues_tbl where RecordID is primary key. Please note that MergeRecordID contains mutiple Record IDs.
Step 1: I would like to sort based on MergeRecordID. In above example we will get 4 records.
Step2: All records should have the Status Invoiced. If the value in column Stattus is other than 'Invoiced', I should skip that mergerecordid i.e. I should not take this Record into consideration. Then I will go for another mergerecordid/recordids combination. I have written query as shown below but its not giving me desired results.
SELECT RECORDID,MERGERECORDID,STATUS,STATUSTIME
FROM BE242.BILLINGVALUES_TBL BV1
WHERE BV1.MERGERECORDID IN ( SELECT BV2.MERGERECORDID
FROM BILLINGVALUES_TBL BV2
WHERE BV2.RECORDID = BV1.RECORDID
AND BV2.MERGERECORDID = BV1.MERGERECORDID
AND BV2.STATUS IN ('Invoiced', 'Cancelled') )
ORDER BY BV1.MERGERECORDID DESC;
Please any one help me out as soon as possible?
for me it looks like your selecting the table twice although it isn't necessary in this scenario.
if you just want to have all record which have Status equal to Invoiced or Cancelled, than a simple
SELECT RECORDID,MERGERECORDID,STATUS,STATUSTIME
from BILLINGVALUES_TBL
where STATUS IN ('Invoiced', 'Cancelled')
should do it.
Otherwise, try to check the status not only in inner select, also check it in surrounding select.

check if condition is met then do a lookup

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