How I get the maximum and minimum value in a query

Hello Goodnight. I consult them again because for more than I try to do it, I just could not.
I want to obtain the maximum and minimum number of a query, when doing it directly to my database the sentence obtains the correct data, when I do it in ireport I do not obtain the correct data.
The query in my DB is:
select max (age)
from table_name
where date_register = '2018-08-02'
and field_name like 'Jhon'
and last_name = 15
I wrote it in different ways, in a dataset with a table and it shows me a list of the same items as in the detail band. Place directly the max field of the dataset and tell me that you can not find the field. Incerte the query in the main report query in this way:
select max (age), as name, table.lastname as lastname ... etc
from table_name
where date_register = '2018-08-02'
and field_name like 'Jhon'
and last_name = 15
group by yhe_same_fields_of_select
order by age
and I get the value of the first row.
I tried to use a variable with calculation lowest and highest and it says null,
to the same variable I put an initial value expression $ f {age} and I returned the first value of the first row.
If you can help me by telling me how I can get the maximum and minimum value in a query I will be very grateful.'s picture
Joined: Sep 13 2017 - 6:59pm
Last seen: 1 month 3 weeks ago

2 Answers:


Ok solution and outcome depends on what your want to achive at the end of the day. 
So if you want your database to return 1 row with min and max then you can make use of sub queries in your select statement.  You did not mention which DB you are using. 
So example in Oracle:
select ( select MAX(number_column) from Your_table ) as MaxNo,
( select MIN(number_column)from Your_table ) as MinNo
From Your_table where Rownum < 2

This would return 1 row with MaxNo column with max value and MinNo with min value. 

You could use ORDER BY age without max function and the first row would be your max and last your min, almost as you did above, just without max function and group by. 

Or if you want jasper to do this, you need to understand that each row returned from DB will be iterated through. So 3 variables. I created a report below that connect to oracle datasource and use Dual to stump data. You need to update the datasource to your DB or update sql if using another DB. 

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version using JasperReports Library version 6.4.1  -->
<jasperReport xmlns="" xmlns:xsi="" xsi:schemaLocation="" name="report name" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="af88d3eb-c1fe-4d76-9a0c-e7394dc2ddb4">
    <property name="ireport.zoom" value="3.4522712143931042"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <property name="" value="UPDATE_HERE"/>
    <property name="" value=""/>
    <style name="style1" isPdfEmbedded="true"/>
        <![CDATA[Select * FROM 
(select 10 as age , 'Green' as Name from DUAL union
select 15 as age , 'Red' as Name from DUAL union
select 25 as age , 'Yellow' as Name from DUAL union
select 2 as age , 'Blue' as Name from DUAL )]]>
    <field name="AGE" class="java.lang.Integer"/>
    <field name="NAME" class="java.lang.String"/>
    <variable name="Max" class="java.lang.Integer" calculation="Highest">
        <variableExpression><![CDATA[$V{Variable_2} > $F{AGE} ? $V{Variable_2} : $F{AGE}]]></variableExpression>
    <variable name="Min" class="java.lang.Integer" calculation="Lowest">
        <variableExpression><![CDATA[$V{Variable_2} > $F{AGE} ? $V{Variable_2} : $F{AGE}]]></variableExpression>
    <variable name="Variable_2" class="java.lang.Integer" calculation="Highest">
        <band splitType="Stretch"/>
        <band height="50">
                <reportElement x="190" y="10" width="100" height="30" uuid="9a16ad60-3e8b-415a-b0b7-3b024797f48e"/>
                <reportElement x="450" y="10" width="100" height="30" uuid="8949c50d-a9db-4f93-b5c3-0a4503acfd37"/>
                <reportElement x="320" y="10" width="100" height="30" uuid="224af22d-cb1a-4922-9d27-6c62bbaf7478"/>
                <reportElement x="10" y="10" width="100" height="30" uuid="41a78580-9280-4134-ad54-7cbb69a906e5"/>
        <band height="100" splitType="Stretch"/>

joseng62's picture
Joined: Dec 5 2014 - 2:43am
Last seen: 3 days 20 hours ago

Thanks joseng62 for your answer, I use DB postgreSQL, I did something similar to what you tell me, now I will try to do them as you tell me.
One more question, in Ireport to create a vartiable, in options has the field calculation and one option is lower and another Highest, these options serve my purpose?
On the other hand I would like a tip too: When using a sub query, would it be better to do it separately or in the main head?
My idea is the following:
/ ************************************************ ****************************** /
Page Header
                                           here: $ {variable1} max = min? "max": "max-min"
/ ************************************************ ***************************** /
I want to put a field, that write in value of maximum-minimum and if they are equal, only the value. But as I have tried it gives me a table with all the values or it gives me an erroneous value.
First of all, Thanks.

PD: The cogigo that you put me, I'm analyzing it and I'm going to adapt it to use it. Thank you.'s picture
Joined: Sep 13 2017 - 6:59pm
Last seen: 1 month 3 weeks ago