Home Answers Viewqa Hibernate sum and avg function using HQL.

 
 


ratna rathor
sum and avg function using HQL.
1 Answer(s)      a year ago
Posted in : Hibernate

How to use sum and avg function in HQL?

View Answers

June 2, 2012 at 6:35 PM


HQL supports many aggregate functions. Sum and avg are some of them.

sum() function returns summation of specified field in the table.

avg() function returns average of specified field in the table.

Example:

package net.roseindia.main;

import net.roseindia.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class DistinctCountHQL {
    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        String sumHql = "Select sum(emp.salary) FROM Employee emp";
        String avgHql = "Select avg(emp.salary) FROM Employee emp";
        Query sumQuery = session.createQuery(sumHql);
        Query avgQuery = session.createQuery(avgHql);
        System.out.println("Sum of all salary : " + sumQuery.list().get(0));
        System.out.println("Average of salary : " + avgQuery.list().get(0));

    }
}

Output:

Hibernate: select sum(employee0_.salary) as col_0_0_ from employee employee0_
Sum of all salary : 275000
Hibernate: select avg(employee0_.salary) as col_0_0_ from employee employee0_
Average of salary : 68750.0

Description: This example calculates sum of salary as- sum(emp.salary) and average of salary as- avg(emp.salary)









Related Pages:
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
Hibernate Avg() Function (Aggregate Functions)
Hibernate Avg() Function (Aggregate Functions)  ... to use the avg() function. Hibernate supports multiple aggregate functions. When they are used in HQL queries, they return an aggregate value ( such as avg
Hibernate avg() Function
Hibernate avg() Function In this tutorial you will learn about the HQL aggregate avg() function in hibernate. An avg() function in HQL performs... that will demonstrate you how to embed the HQL avg() function in between your query
pie chart flex and sum from hql request
pie chart flex and sum from hql request  hi, i work with flex and j2ee project.i have to make a function wich return list of value: print("public... d.intituleDeleg,SUM(s.usagePedag ),ROUND((((SUM(s.usagePedag ))/(SUM(s.ordidispo)))*100),0
Sum of seris
Sum of seris  Program to find the sum of the following series using a function declaration. sum=x-(xxx)/3!+(xxxxx)/5!-(xxxxxxx)/7!+.....(xx....x)/n!. Where n and x are entered from the keyboard
Calculate sum and Average in java
(); int num = Integer.parseInt(data); int sum = 0; float avg...; } avg = sum / num; System.out.println("Sum- " + sum...Calculate sum and Average in java  How to calculate sum and average
HQL, Hibernate Query Language
columns. But in case of HQL we write queries using the persistence object... the programmer to write Query using the persistence entity. The HQL is fully... using HQL. HQL allows the developers to write queries in object oriented
SQL Avg Function
SQL Avg Function       SQL Avg Function is used to compute the average records value in a column... 'SQL Avg Function'. To understand and grasp the example we create a table 'stu
JPA Avg Function
JPA Avg Function       In this section, you will learn the avg of aggregate function. This method calculate an average of  given data. JPA Avg() function
Hibernate criteria query using avg()
Hibernate criteria query using avg()  How to calculate average in Hibernate criteria query using Projection?   You can calculate average of any numeric values in hibernate criteria by using projection. Projection
Hibernate Projection Example (Sum)
function like: sum() using hibernate projection.  The following example to calculate the sum of invested_amount to the Insurance table  Table Name...Hibernate Projection Example (Sum)     
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
SQL Avg Group By
SQL Avg Group By       SQL Avg Group By helps you to find out average value of numeric field... In this Tutorial we help you to grasp SQL Avg Group By by giving
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
Using Min, Max and Count on HQL.
Using Min, Max and Count on HQL.  How to use Min, Max and Count on HQL
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
Find sum of series
Find sum of series This section illustrates you how to find the sum of n terms of the series using recursion. For this purpose, we have created a recursive function that takes the parameter 'n' up to which the sum of the series
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 Query Language
are: avg(...), sum(...), min(...), max(...) ...; Hibernate Query Language or HQL for short is extremely powerful query language. HQL is much like SQL  and are case-insensitive, except
Using sum() in hibernate criteria.
Using sum() in hibernate criteria.  How to calculate sum() in hibernate criteria by using projection?   You can calculate sum of any numeric value in hibernate criteria by using projection. Projection is an interface
JPA Sum Function
JPA Sum Function       In this section, you will learn about sum function of aggregate function of JPQL (Java Persistence Query Language). The sum function to calculate the sum
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
MySQL Aggregate Function
are the Aggregate function: Name Description AVG... | +----------------------+-------------+-------------+ Now, The 'SUM' function is used to sum all... MySQL Aggregate Function   
Display Sum of Table Column Using In JSP
Display Sum of Table Column Using In JSP... an application to Display the sum data of a Table column for a specific Date. We created five file sum_count.jsp, resultForm.jsp, result_count.jsp, sumcount.java
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.... This function can be simply written with the select clause, however it can be used
SQL Avg Group By
_name,GROUP_CONCAT(marks) as marks, sum(marks)as total ,avg(marks) as per from... SQL Avg Group By       SQL Avg Group By helps you to find out average value of numeric field
HQL
HQL  What is the HQL in hibernate ? Explain the use of HQL.   Hi Friend, Please visit on this link....... Hibernate Query Language
using function
using function   using a function to find the value of v from the foiiowing expression 1v=1u+1*f
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... Description: This example counts the number of employee having distinct salary by using
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
SQL Aggregate Sum
SQL Aggregate Sum       The SQL Aggregate Sum Function is a part of Aggregate Function that is used to compute sum of the numeric field in a table. Understand with Example
Sum - JSP-Servlet
the total sum of items from database using Java servlet or Jsp and html form. It should display the affected rows and the total sum of all in a seperate column. Please the database I'm using is Microsoft Sql 2000 not MYSQL. Thank you all
Hibernate Min() Function (Aggregate Functions)
the Min() function. Hibernate supports multiple aggregate functions. When they are used in HQL queries, they return an aggregate value ( such as avg(...), sum... Hibernate Min() Function (Aggregate Functions)  
Hibernate Max() Function (Aggregate Functions)
to use the Max() function. Hibernate supports multiple aggregate functions. When they are used in HQL queries, they return an aggregate value ( such as avg... Hibernate Max() Function (Aggregate Functions
Hibernate criteria sum.
Hibernate criteria  sum. Here, we will introduce you to about the sum() function . It returns the sum of total value of column. Syntax : Criteria... message as shown below: Hibernate: select sum(this_.fee
SQL NOW() Function
( ) Function. In this Tutorial, we create a table 'Stu_Table' using create..., GROUP_CONCAT(marks) as marks, sum(marks)as total ,avg(marks) as per, now() from... SQL NOW() Function      
HQL Query in execute method of Struts2
data from database using hql. And where I need to write the hql query...HQL Query in execute method of Struts2  I am making login page in struts2 and using hibernate . Now I wants to pull user name and password from
using function
using function  write a program using function overloading to accept any sentence and print vowels and consonants sepratly   Here is an example that accepts a sentence from the user and display the vowels and consonants
sum
sum  a program to find the sum of the alternative diagit of it ex- no=123456 sum=1+3+5=9
Hibernate Named HQL using XML Mapping
In this section, you will learn executing Named HQL written in XML Mapping file
sum
; sum=sum+s; if(i==n){ System.out.print(s...+"+"); } System.out.print("\nSum of the series : "+sum
Sum of a Number using Swing
Sum of a Number using Swing       In this section, you will learn how to sum of a number using swing...;); Step: Create a file "Calculate.java" to sum of a number using
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
SQL Aggregate Sum
SQL Aggregate Sum       The SQL Aggregate Sum Function is a part of Aggregate Function that is used to compute sum of the numeric field in a table. Understand with Example
SQL ROUND Function
SQL ROUND Function       Round Function in SQL is used to round a numeric field to the nearest... SQL Round Function. In this Tutorial, we create a table 'Stu_Table'. 
sum
sum
sum
sum
sum