Ignore nulls from multi-select input controls? - jasper-reports

I have a report with several input controls that are used to populate another input control.
My input controls:
GPI - a multi-select input control that has product codes. The parameter is a collection.
NAME_LOOKUP - a single-value text box where the user can type a product name. The parameter is a string.
NDC - a list of drug codes that obtains values based on what is input in GPI and NAME_LOOKUP.
The query that populates NDC has this WHERE clause:
WHERE (
REGEXP_LIKE(DESCRIPTION, $P{name_lookup}, 'i')
OR REGEXP_LIKE(NDC, $P{name_lookup}, 'i')
)
OR ($X{IN, GPI, gpi})
It works as the $X syntax is designed to -- if the user doesn't select a GPI value from the list, my NDC input control shows all drug codes. However, I only want to show drug codes when a value is actually selected from the GPI input control.
When I try
WHERE (
REGEXP_LIKE(DESCRIPTION, $P{name_lookup}, 'i')
OR REGEXP_LIKE(NDC, $P{name_lookup}, 'i')
)
OR ($X{IN, GPI, gpi} AND $P{gpi} IS NOT NULL)
I get an invalid column type in JasperReports Server for the NDC input control.
When I try
WHERE (
REGEXP_LIKE(DESCRIPTION, $P{name_lookup}, 'i')
OR REGEXP_LIKE(NDC, $P{name_lookup}, 'i')
)
OR ($X{IN, GPI, gpi} AND $P!{gpi} IS NOT NULL)
I get a missing expression error.
What can I do to limit the NDC input control's results to just the selected GPI, and not all results?

I just found this while experiencing the same problem, I finally solved it using a second parameter holding the lenght of the list:
<parameter name="potentially_empty_list" class="java.util.Collection"/>
<parameter name="num_of_list_elements" class="java.lang.Integer" isForPrompting="false">
<defaultValueExpression>
<![CDATA[$P{potentially_empty_list}.size()]]>
</defaultValueExpression>
</parameter>
And the SQL query would then be:
[... select etc...]
WHERE $X{IN,mysql_column_name, ist}
AND $P{num_of_list_elements} > 0
which for an empty list would be sent to mysql as:
WHERE 0 = 0 #(that's how jasper parses $X{IN..} for an empty collection
AND 0 > 0 # don't match anything :)

There is an interesting alternative because $X{NOTIN, ...} also evaluates to 0=0 in case of empty list.
Therefore you can do like this:
($X{IN, cs.companyid, companyIds}
AND NOT
($X{NOTIN, cs.companyid, companyIds}))

Related

Finding a value in a multi select list parameter in report?

I have a multi select input parameter for a report.
We know how to find whether a values is present in that parameter or not by using
$X{IN, colName, paramName}
the above expression in WHERE condition clause.
That is working fine.
But how will we find whether a variable ( let say having value 3 ) is present in parameter ( same multi select list let say [ 1, 2 ,3, 5] ) or not.
By validating the above condition i want to display something in report's static text field.

How to filter rows with null values in any of its columns in SSRS

I want to filter out the output without rows containing null values or blank columns. I am using SQL Server 2012 there is no option named 'Blank' as in SS2005 where I can filter the rows. I also tried following expression but it gives me error or not showing correct output
=IsNothing(Fields!ABC.Value)!= True
=Fields!ABC.Value = ''
Please suggest the solution.
Pull up the tablix or group properties
Switch to "Filters"
Add a new filter
Set the expression to:
=IsNothing(Fields!YourFieldHere.Value)
Set the type to "Boolean" (see screenshot below) otherwise you'll get a "cannot compare data of types boolean and string" error.
Set the value to false
This works for filtering both rows and groups.
We should use the isNothing method in the Expression, change the Text to Boolean
and then Value will be "True"
for example:
Expression
=IsNothing(Fields!TestA.Value)<>True
(Expression type should be Boolean)
Operator
=
Value
=True
Edit the SQL query, so that it will not return NULL values in the column to group on, but let it return a dummy value; for example: ISNULL(columnA, 'dummy')
In the column group definition add a filter: ColumnA <> 'dummy'.

Traverse Ireport Input control paramter of type collection(mutiselect parameter)

I have a Oracle query where I want to check content of a Ireport input control parameter of data type "collection" like below the SQL
select devlocview.floorname FROM AlarmDeviceView alrmDevView INNER JOIN devicelocationview devLocView ON devlocview.oid=alrmdevview.deviceoid
LEFT OUTER JOIN DEVICEGROUPVIEW devGrpView ON devlocview.oid=devgrpview.deviceoid
LEFT OUTER JOIN RackCapacityView rackCapView ON devlocview.RACKID=rackCapView.oid WHERE
(
( $P{building.name} IS NOT NULL AND devlocview.buildingname = $P{building.name} )
OR
( $P{building.name}='All Buildings' AND devlocview.buildingname IN (SELECT buildingname FROM cdmr.devicelocationview ) )
)
AND $X{IN, devlocview.floorname, floor.name}
Here
"floor.name" is the multiselect(collection type) inputcontrol parameter of Ireport which contains the value selected by the user in UI
"devlocview.floorname" SQL query output
Now I want to do something like
IF USER SELECTS "ALL BUILDING" IN UI i.e. IF floor.name contains "ALL BUILDING"
THEN DO SOMETHING
ELSE
THEN DO SOMETHING
The problem I am facing is I am not able to traverse and check the contents of Ireport Input control parameter "floor.name" which is of type "collection"

Conditional Sum (variable)

A report in iReport (4.0.1) with various fields includes: $F{value} (Integer) and $F{created_at}.
I'd like to calculate variables that would give:
the sum of $F{value} when $F{created_at} is before a given date
the sum of $F{value} when $F{created_at} is after a given date
Any idea how this could be done?
You will have to use two different variables to do this. For your variables, use something like this in the 'Variable Expression'. The Date class also has an after() function. If the expression evaluates to true $F{value} will be added, otherwise 0 will be added.
$F{created_at}.before($P{givenDate}) ? $F{value} : 0
To use a variable to sum, you need to change the calculation type to "Sum". The default reset type, report will sum values over the entire report. The other reset types work the same way just over different sections of the report (column, page or group).
Here is the XML for the "before" case:
<variable name="sumValueCreatedBefore" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[F{created_at}.before($P{givenDate}) ? $F{value} : 0]]></variableExpression>
</variable>
there is another solution for that : write sub query in the select statment
like
Select
(select sum(Fieldname) from tablename where dategiven date) as aftersum
from tablename
where conditions

String comparisons in JasperReports expressions

A database field named income_source is queried using:
SELECT * FROM table_name WHERE income_source LIKE "salaried%"
This retrieves income_source values with a "salaried" prefix. In iReport, the PrintWhenExpression value for the field is set as:
$F{income_source}.equals("Salaried")? Boolean.TRUE:Boolean.FALSE
Why does the report output differ from the SQL output?
There are a few problems:
The value "salaried%" in the SQL differs from the value of "Salaried" in the expression.
The value "salaried%" uses the % to match all text after the letter d.
There is a bit of redundancy in the PrintWhenExpression.
Try the following expression:
$F{income_source}.startsWith( "salaried" )
Or:
$F{income_source}.trim().toLowerCase().startsWith( "salaried" )
One of those should work. You will also want to ensure Blank when null is checked. Otherwise, the expression becomes:
$F{income_source} == null ? Boolean.FALSE :
$F{income_source}.trim().toLowerCase().startsWith( "salaried" )