Quantcast
Channel: The Other Side of MyGeekDaddy » SQL
Viewing all articles
Browse latest Browse all 2

Options for managing NULL results in BIRT

$
0
0

One of the challenges a report writer can have is accurately having a report show data when that data doesn’t exist. The example report is finding the ASSETNUM, SITEID, and PRIORITY from the ASSET table. The data has a mix of actual values for ASSET.PRIORITY and some with no value, or null values. Normally a report writer would make the Fetch Method read like this:

if (!maximoDataSet.fetch())
return (false);

// Add a line for each output column
// The specific get method should match the data type of the output column
row["assetnum"] = maximoDataSet.getString("assetnum");
row["siteid"] = maximoDataSet.getString("siteid")
row["priority"] = maximoDataSet.getInteger("priority");

return (true)

But if one of these fields has no information, or NULL data, the output of a simple list report would look like this:

In the case of the ASSET.PRIORITY field, a NULL value and a zero ’0′ are both valid values but these two values are not the same. Notice that some of the results are shown as a value of ’0′, but in the Maximo UI, these assets would show an empty/null value in the ASSET.PRIORITY field. Having ’0′ show up in the report for NULL values could lead to incorrect decisions or assumptions of what is going on with the Maximo data.

There are two approaches a report writer can take to handling NULL data in BIRT report results. One method allows a report to show the missing data. The other method is to substitute null/missing data with a known, or specific, value.

Fetch Method – Show NULL data

The first method is to include an If-Else statement in the Fetch Method of the report file. An example of the Fetch Method in a report is:

if (!maximoDataSet.fetch())
return (false);

// Add a line for each output column
// The specific get method should match the data type of the output column.
row["assetnum"] = maximoDataSet.getString("assetnum");
row["siteid"] = maximoDataSet.getString("siteid");

if (maximoDataSet.getString("priority") != null)
{
row["priority"] = maximoDataSet.getInteger("priority");
}

return (true);

The If-Else statement says if value of ASSET.PRIORITY exists, then apply the value of ASSET.PRIORITY, else return nothing, or a NULL value. The output of the simple list report using this method is:

Notice how the report now shows blank values for ASSET.PRIORITY fields that contained a NULL value. This gives end users a completely different view of the data as compared to the default view in the previous section.

COALESCE SQL function – Show alternate NULL value

The other method is adding a default value in the event a returned result is NULL. [1]. Depending on what database version you use, the syntax will be different.

Database Syntax
SQL Server isnull(primary, secondary)
DB 2 coalesce(primary, secondary)
Oracle nvl(primary, secondary)

This function allows a report writer to supply a known alternate data value in the event the record returns a NULL value. In the Open Method, a report writer can add the proper function to the select statement. An example is shown below.

// Add query to sqlText variable.
//sqlText = "select assetnum, priority, siteid "
sqlText = "select assetnum, siteid, isnull(priority, 1) as priority "
+ " from asset"
// Include the Maximo where clause
+ " where " + params["where"]
;

maximoDataSet.setQuery(sqlText);

The report results using this method are shown below. Note how the previous NULL values are now set to a value of ’1′.

Have any questions? Hit me up on Twitter at @MyGeekDaddy


Update: I got some feedback asking about the syntax for using the maximoDataSet.getNullValueFunction. The Report Developer Guide is a little misleading in that it makes it appear a report writer could just add the function as part of the main select statement in the Open Method, like this:

// Add query to sqlText variable.
//sqlText = "select assetnum, priority, siteid "
sqlText = "select assetnum, siteid, " + maximoDataSet.getNullValueFunction("priority", "1")
+ " from asset"
// Include the Maximo where clause
+ " where " + params["where"]
;

maximoDataSet.setQuery(sqlText);

Using this syntax will cause BIRT to error out and not return any results. The reason why is the maximoDataSet.getNullValueFunction function has to be used in a sub-select statement in the Open Method. A correct example of the report would look like this:

/ Add query to sqlText variable.
//sqlText = "select assetnum, priority, siteid "
sqlText = "select asset.assetnum, asset.siteid, "
+ " ( "
+ " select " + maximoDataSet.getNullValueFunction("asset.priority","1") + " from asset a "
+ " where a.siteid=asset.siteid and a.assetnum=asset.assetnum and a.assetid=asset.assetid) as priority "

+ " from asset"
// Include the Maximo where clause
+ " where " + params["where"]
;

maximoDataSet.setQuery(sqlText);

Using the Open Method statement from above passes the following select statement when the report is run: [2]

select asset.assetnum, asset.siteid, ( select isnull(asset.priority, 1) from asset a where a.siteid=asset.siteid and a.assetnum=asset.assetnum and a.assetid=asset.assetid) as priority from asset where asset.siteid=’BEDFORD’

Note how the maximoDataSet.getNullValueFunction automatically converted the sub-select to a isnull(asset.priority, 1) statement like it was hard coded in the previous section. The difference is if the same report was run against a Maximo instance on a DB2 database, the SQL query passed by the report would look like this:

select asset.assetnum, asset.siteid, ( select coalesce(asset.priority, 1) from asset a where a.siteid=asset.siteid and a.assetnum=asset.assetnum and a.assetid=asset.assetid) as priority from asset where asset.siteid=’BEDFORD’

Note how the maximoDataSet.getNullValueFunction will change the select statement based on type of database being used. This is why using the maximoDataSet.getNullValueFunction is a best practice because it makes the BIRT report database agnostic.


  1. The best practice method is to use the maximoDataSet.getNullValueFunction. Details on this BIRT function can be found in the Maximo 7.1 Report Developer Guide. The function is the same for BIRT 3.7.1 in Maximo 7.5.0.3+.  ↩

  2. The SQL statement can be found in report log when DEBUG logging is enabled.  ↩


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images