How to loop query results in JasperReports and to print single value based on a condition


Hi All,

Let's say the query is as follows:

SELECT customer_id, customer_name, metric_name, metric_description, target_value, real_value
FROM customers_overview
WHERE customer_id = $P{customer_id}
AND reporting_year = 2019

It'll give me a few rows, one per each metric_name, e.g. AVAILABILITY, TRAFFICOB, STORAGEDB, TRAFFICCW etc. All metric values delivered as a query result are related to this customer.

In JasperStudio I already have a kind of "written in stone" report with the following hardcoded table items: Availability of the platform (%), Total database storage (GB) etc. So I don't want to use table nor list to dynamically render all the rows as available in the query result.

My question is how to display the $F{real_value} of the row where metric_name is STORAGEDB next to the label 'Total database storage (GB)' in the report. Please consider my query will return many rows.

In pseudo-code I need something like this:

foreach(rows as row){
   print row.metric_name == "STORAGEDB" ? row.real_value : "";

... and such loop to repeat for each and every item I have hardcoded on my report.

dusandevic's picture
Joined: Aug 16 2013 - 5:37am
Last seen: 1 month 1 week ago

1 Answer:


Not sure about your question. JasperReports will loop through all rows in the query.

If you just want to enter a condition to print $F{real_value} when $F{metric_name}.equals("STORAGEDB", you can just enter a ternary operator in the Expression.

$F{metric_name}.equals("STORAGEDB") ? $F{real_value} : ""

hozawa's picture
Joined: Apr 24 2010 - 4:31pm
Last seen: 27 min 9 sec ago