Jump to content
Changes to the Jaspersoft community edition download ×

Unique or Distinct Count?

2005 IR Help

Recommended Posts

By: Todd - tberman333

Unique or Distinct Count?

2005-04-20 11:16

I need to count the number of unique records in a group. I can't use the count variable because the field that I need to count is often repeated for 6 or 7 records. Does anyone know a way to do a distinct count in iReports?

Thanks for the help!





By: Paul Meshkovsky - meshpaul

RE: Unique or Distinct Count?

2005-04-20 19:16

Interesting question.


I doubt it is possible but assuming your data is sorted you may be able to use conditional expression ex

Crete Varible NewVar and Varible OldVar and UniqueCount(Integer)

Where NewVar Is Constant Value Of your data and OldVar initially 0 or null depending on your data type


If NewVar!=OldVar






If not you may have to Create Scripplet Object and Process data through it methods that support above functionality..





By: David - dnarmitage

RE: Unique or Distinct Count?

2005-04-21 16:30

I can give you a distinct count for the report, but I haven't yet hacked a per group version:


Define a parameter 'distinctMap' as 'java.util.Map' with default value expression 'new java.util.HashMap()'.


On any field in detail add the following print when expression:

'$P{distinctMap}.put($F{distinctfield}, "") == null ? true : true' where distinctfield is the column to count distinct.


Then, where required, add a field with the following textfield expression as Integer:



This basically uses the property of a HashMap that replaces a key value that already exists, rather than adding a new entry, This means that the size of the map is our count of distinct entries (the value part of the hashmap doesnt matter to us). Additionally, if you add .toString() to the key value it will not count null values.


If you use this at group level it will give a running distinct count (????).


I have tried extending this using Variables rather than Parameters, but I can't seem to stop the hashmap being reset on every line of data, no matter what combinations I try. If anyone can correct my stupidity I would be grateful.





By: Giulio Toffoli - gt78

RE: Unique or Distinct Count?

2005-04-22 00:37


You have to use a scriptlet or an accessor class that provides a function like:


MyClass.isRepeatedValue( value )














By: David - dnarmitage

RE: Unique or Distinct Count?

2005-04-22 04:14

Further to my previous note, the following can achieve per Group distinct counts using scriptlet as follows:


Add following to scriptlet class (using scriptlet editor):

static HashMap distinctMap = new HashMap();


Add the following methods to the class in <Imports and global declarations> for moment:

public static Object distinctMapPut(Object pKey, Object pValue)


return distinctMap.put(pKey, pValue);


public static Integer distinctMapGetSize()


return new Integer(distinctMap.size());



In beforeGroupInit add the following (obviously Dummy will changed to be your group name):

if (groupName.equals("Dummy"))





Then, as before, create a print when on an appropriate field as (testScriptlet should be changed to be your scriptlet name, fieldname to the distinct item required):

test2Scriptlet.distinctMapPut($F{fieldname}, "") == null ? true : true


Then your field to present the value becomes:



This should behave correctly on group breaks. If you need another separate distinct count, then it will require another set of the scriptlet bits.

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Popular Days

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...