ratna rathor
Using Min, Max and Count on HQL.
2 Answer(s)      4 years and 2 months ago
Posted in : Hibernate

How to use Min, Max and Count on HQL ?

Ads

View Answers

June 1, 2012 at 7:13 PM


HQL supports many aggregate functions. Min,max and count are some of them.

min() function returns minimum value of specified value in the table.

max() function returns maximum value of specified value in the table.

count() function returns number of rows of the table.

Example:

package net.roseindia.main;

import java.util.*;
import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class AggregateFunctionHQL {
    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        String minHql = "Select min(emp.salary) FROM Employee emp";
        String maxHql = "Select max(emp.salary) FROM Employee emp";
        String countHql = "Select count(*) FROM Employee emp";

        Query minQuery = session.createQuery(minHql);
        Query maxQuery = session.createQuery(maxHql);
        Query countQuery = session.createQuery(countHql);
        System.out
                .println("Minimum salary in list : " + minQuery.list().get(0));
        System.out
                .println("Maximum salary in list : " + maxQuery.list().get(0));
        System.out.println("Number of Employee : " + countQuery.list().get(0));
    }
}

Output:

Hibernate: select min(employee0_.salary) as col_0_0_ from employee employee0_
Minimum salary in list : 15000
Hibernate: select max(employee0_.salary) as col_0_0_ from employee employee0_
Maximum salary in list : 220000
Hibernate: select count(*) as col_0_0_ from employee employee0_
Number of Employee : 4

Description: Here min(salary) returns the minimum salary in the employee table using HQL.

max(salary) returns the maximum salary in the employee table using HQL.

count(*) returns the number of employee in the table.



September 26, 2012 at 8:56 PM


Suppose that rather than the simple SELECT MAX(emp.salary)... I want the whole Employee object, with the max. salary, how do I write that query?

Something like: SELECT e from Employee e WHERE e.salary = MAX(e.salary) Except that's wrong!










Related Tutorials/Questions & Answers:
Using Min, Max and Count on HQL.
Using Min, Max and Count on HQL.  How to use Min, Max and Count on HQL
how to calculate max and min - RUP
and minimum using java language.  Hi friend, In Math class having two function max and min in java. public class MathMaxMin { public static...how to calculate max and min  hye!i'm a new for the java and i want
Advertisements
Count distinct value in HQL.
Count distinct value in HQL.  How do you count distinct value in HQL?   count(distinct fieldName) returns number of distinct value... = sessionFactory.openSession(); String countHql = "Select count(distinct salary
Mysql Min Max
Mysql Min Max       Mysql Min Max is useful to find the minimum and maximum records from a table. Understand with Example The Tutorial illustrate an example from Mysql Min Max
how to calculate max and min in the loop - Java Beginners
how to calculate max and min in the loop  thanks coz giving me... main(String a[]) throws IOException { int max,min; max=Integer.parseInt(a[0]); min=Integer.parseInt(a[0]); for(int i=0;imax) max=Integer.parseInt
hibernate criteria Max Min Average Result Example
hibernate criteria Max Min Average Result Example In this Example, We... the Projections class max, min ,avg methods. Here is the simple Example code files...("Max Salary is : "); System.out.print(row[i]); System.out.printf("\n
SQL Max Count
SQL Max Count       SQL Max Count is used to return the maximum records value of the specified column... 'SQL Max Count'. To elaborate example we create a table 'Stu'. The create table
Hibernate criteria query using Min().
Hibernate criteria query using Min().  How to find minimum value in Hibernate criteria query using Projection?   You can find out minimum value in hibernate criteria by using projection. Projection is an interface
Delete Query using HQL
Delete Query using HQL  Can we write 'AND' with 'WHERE' clause in delete query using HQL? For example: delete from table name where field1 = value1 and field2 = value2   The example query i have written
Hibernate min() Function
Hibernate min() Function This section contains an example of the HQL min() function. min() function in HQL returns the smallest value from the selected... will demonstrate you how to embed the HQL min() function within your code
Hibernate criteria query using Max()
Hibernate criteria query using Max()  What is Hibernate criteria query using Max()?   You can find out maximum value in hibernate criteria by using projection. Projection is an interface and a class
sum and avg function using HQL.
sum and avg function using HQL.  How to use sum and avg function in HQL?   HQL supports many aggregate functions. Sum and avg are some of them. sum() function returns summation of specified field in the table
HQL
HQL  What is the HQL in hibernate ? Explain the use of HQL.   Hi Friend, Please visit on this link....... Hibernate Query Language
Max()
Max()  How to find maximum values A101,A107,A108 from a table in sql
Max()
Max()  How to find maximum values A101,A107,A108 from a table in sql
Hibernate HQL query by using like operator.
Hibernate HQL query by using like operator.  How use like operator in HQL?   HQL stands for Hibernate Query Language, provided by Hibernate... = sessionFactory.openSession(); String hql="SELECT emp.name, emp.salary, emp.dateOfJoin
Hibernate max() Function
Hibernate max() Function In this tutorial you will learn about how to use HQL max() function. max() function in HQL returns the largest value from... below will demonstrate you how to embed the HQL max() function within your code
column count using metadata
GET COLUMN COUNT USING "RESULTSETMETADATA"   In this program ,we will get column count using "resultsetmetadata ".Using resultset interface and metadata interface ,we can get properties of column
Case-insensitive search using Hibernate Query Language(HQL).
Case-insensitive search using Hibernate Query Language(HQL).  What is Case-insensitive search using Hibernate Query Language
MySQL Aggregate Function
in the MySQL Query. In this example we use the aggregate function ('COUNT', 'MIN', 'MAX', and 'SUM') in the MySQL Query. The group (aggregate) functions...; SELECT designation, MIN(salary) , MAX(salary) FROM employee GROUP
Finding MAX value from emp_id column using SQL query?
Finding MAX value from emp_id column using SQL query?  I need to select maximum of the Emp_id values from SQL server 2005 table.I was using... of emp id from the table.(Actually it picks up Max value using by looking at 1st
How to calculate minimum in Hibernate using the min() Function
How to calculate minimum in Hibernate using the min() Function The min... in Java. The min() function is part of HQL and follows the semantics of Hibernate Query Language. min() function in HQL returns the smallest value from
how to count words in string using java
how to count words in string using java  how to count words in string using corejava   Hi Friend, Try the following code: import...: "); String st=input.nextLine(); int count=0
count values using jsp - JSP-Servlet
count values using jsp  hai deepak i have a query i am... and subheadings i want to get the count of textboxes which are heading under that how... by array count can you help me to sort out this problem expecting your favourable
Min Value
Min Value   How to access min value of column from database table... min(id) from QuestionModel result_info"; Query query = session.createQuery(selectQuery); System.out.println("Min Value : " +query.list().get(0
Hibernate count() Function
Hibernate count() Function In this tutorial you will learn how to use the HQL count() function count() function counts the maximum number of rows in a table however it doesn't count the null values of the specified column
HQL query
HQL query  how to select only password based on username using HQL query from login table where uname and paswrd are two column names
Hibernate Min() Function (Aggregate Functions)
(...), min(...), max(...) , count(*), count(...), count(distinct ...), count(all... Hibernate Min() Function (Aggregate Functions)  ... the Min() function. Hibernate supports multiple aggregate functions. When
SQL Aggregate Functions List
List Queries. The Aggregate Function include the average, count, min, max, sum etc... | +------+---------+-----------+ 5 rows in set (0.00 sec) COUNT Functions:  The  count (id) Function is a aggregate function that return the sum number of records
HQL in hibernate ? Explain the use of HQL.
HQL in hibernate ? Explain the use of HQL.  What is the HQL in hibernate ? Explain the use of HQL.   HQL stands for Hibernate Query...(); Session session = sessionFactory.openSession(); String hql="SELECT
convert into hql
convert into hql   String sqlListSurveys= "select distinct s.surveyAutoID, s.surveyTitle, date(s.modifiedOn) modifiedOn, s.createdOn..., (select ifnull(max(respondentID),0) FROM response, survey sur where surveyID

Ads

Advertisement