C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
HiveQL - GROUP BY and HAVING ClauseThe Hive Query Language provides GROUP BY and HAVING clauses that facilitate similar functionalities as in SQL. Here, we are going to execute these clauses on the records of the below table: GROUP BY ClauseThe HQL Group By clause is used to group the data from the multiple records based on one or more column. It is generally used in conjunction with the aggregate functions (like SUM, COUNT, MIN, MAX and AVG) to perform an aggregation over each group. Example of GROUP BY Clause in HiveLet's see an example to sum the salary of employees based on department.
hive> use hiveql;
hive> create table emp (Id int, Name string , Salary float, Department string) row format delimited fields terminated by ',' ;
hive> load data local inpath '/home/codegyani/hive/emp_data' into table emp;
hive> select department, sum(salary) from emp group by department; Here, we got the desired output. HAVING CLAUSEThe HQL HAVING clause is used with GROUP BY clause. Its purpose is to apply constraints on the group of data produced by GROUP BY clause. Thus, it always returns the data where the condition is TRUE. Example of Having Clause in HiveIn this example, we fetch the sum of employee's salary based on department and apply the required constraints on that sum by using HAVING clause.
hive> select department, sum(salary) from emp group by department having sum(salary)>=35000; Here, we got the desired output.
Next TopicHiveQL - ORDER BY and SORT BY Clause
|