Data aggregation and sampling

Source: Internet
Author: User
Basic aggregation -- Common Aggregate functions count, sum, AVG, Max, min -- Aggregate functions cannot be nested, for example, AVG (count (*) error! Set hive. map. aggr = true; -- pre-aggregation on the Mapper end improves performance, but consumes a lot of memory # Note: do not directly Select fields that are not present in the group by clause; otherwise, the why? Select name, gender_age.gender, count (*) as row_cnt -- error! From employeegroup by gender_age.gender; how do I select fields that do not appear in group? 1) Use the collect_set function; select gender_age.gender, collect_set (gender_age.age) [0] As random_age, count (*) as row_cntfrom employeegroup by limit; -- select uses multiple Aggregate functions select distinct, AVG (gender_age.age) as avg_age, count (*) as row_cntfrom employeegroup by gender_age.gender; 2) use analysis functions (group by is not required) -- aggregate function and condition function used together -- calculate the average age of male select sum (case when gender_age.gender = "male" then gender_age.age Else 0 end)/sum (case when gender_age.gender = "male" then 1 else null end) as man_age_avg -- 0 = nullfrom employee; select AVG (gender_age.age) as avg_agefrom employeewhere condition = 'male' group by gender_age.gender; # The first method is more efficient than the second method -- calculate age_sum, woman_age_sumselect sum (coalesce (gender_age.age, 0) as age_sum, sum (if (gender_age.gender = "female", gender_age.age, 0) as woman_age_sumfrom EMPL Oyee; -- use distinct in aggregate functions -- count the gender of an employee, select count (distinct gender_age.gender) as sex_uni_cnt, count (distinct name) as name_uni_cntfrom employee; # Note: when count and distinct are used together, using a reducer will reduce the performance. Solution: separately using select count (*) as sex_uni_cntfrom (select distinct gender_age.gender from employee) A; # Note: if a field with a null value is encountered during aggregation, the row is ignored. For example, -- Create Table T as select * from (select employee_id-99 as val1, EMPL Oyee_id-98 as val2 from employee_hrwhere employee_id <= 101 Union allselect null as val1, 2 as val2 from employee_hrwhere employee_id = 100) A; + --------- + -- + | T. val1 | T. val2 | + --------- + -- + | 1 | 2 | null | 2 | 2 | 3 | + --------- + -- + select sum (val1 + val2) from t; -- the second row is ignored. Solution: Select sum (coalesce (val1, 0) + val2) from t; advanced aggregation -- grouping sets: use Union all to connect multiple group Result set. In a job, the more efficient grouping sets of the inner layer are completed to define the data to be implemented by group by in each union all. Select name, work_place [0] As main_place, count (employee_id) as emp_id_cntfrom employee_idgroup by name, work_place [0] grouping sets (name, work_place [0]); -- one parameter, without Union all <=> select name, work_place [0] As main_place, count (employee_id) as emp_id_cntfrom employee_idgroup by name, work_place [0]; select name, work_place [0] As main_place, count (employee_id) as emp_id_cntfrom employee_idgroup by N Ame, work_place [0] grouping sets (name), (work_place [0]); -- Union all connects two group by result sets <=> select name, null as main_place, count (employee_id) as emp_id_cntfrom employee_idgroup by nameunion allselect null as name, work_place [0] As main_place, count (employee_id) as employee employee_idgroup by work_place [0]; select name, work_place [0] As main_place, count (employee_id) as employee_id_cntfr Om employee_idgroup by name, work_place [0] grouping sets (name, work_place [0]), name, work_place [0], (); -- () not grouped # boiled fish: when the content of grouping sets is the column pointed to by the table or record alias, an error may occur, which has been officially fixed. For example, select distinct, gender_age.age, count (name) as distinct employeegroup by grouping, gender_age.age grouping sets (gender_age.gender, gender_age.age); -- summarize the extensions of the rollup: Group, high Efficiency and minimal query overhead. Create n + 1 level aggregation. n is the group column group by a, B, c with rollup <=> grouping set (A, B, C ), (A, B), (a), () -- cube summarizes all possible combinations of the grouping field group by a, B, c with cube -- grouping_id function: select grouping_id, Bin (cast (grouping_id as bigint) as bit_vector, name, start_date, count (employee_id) emp_id_contfrom employee_hrgroup by start_date, name with cube order by start_date; -- having filters the group's datasets and filters out some Unnecessary groups group employees by age. Statistics: age groups of <= 3 select gender_age.age, count (distinct employee_id) as cntfrom employee_idgroup by gender_age.age having CNT <= 1; <=> select. age,. cntfrom (select gender_age.age as age, count (distinct employee_id) as CNT -- the field in the query must be an alias from employee_idgroup by gender_age.age) awhere. CNT <= 3; -- where does not support the UDAF function. An alias ?????? Analytics functions are more flexible, practical, easier, and more powerful than group by functions. They count functions (arg1 ,..., argn) over ([partition by <...>] [order by <...>] [<window_clause>]) function (arg1 ,..., argn): Standard Aggregate functions: Sum, count and AVG support 'distinct '(hive 2.1.0), followed by order by or window clause (hive 2.2.0) for example, you can query the number of people in different departments: Count (distict employee_id) partition by apart analysis function: row_number: Number the elements based on partition by and order, when each group starts from 1, the sorting function (as follows) and lead, log over does not support window opening: ntile (): divides sorted Datasets Bucket, and assign a proper Bucket number to each row, which is suitable for equaling data? Rank (): Numbers of sorted fields in the group, for example, scores 60 60 80 90-> 1 1 3 4 start from 1. The same value numbers are the same dense_rank (): group ID: 60 60 80 90-> 1 1 2 3 cume_dist (): in ascending order, defined as the number of rows whose values are less than or equal to the value of the current row divided by the total number of rows in the partition or query result set percent_rank (): group fields are numbered by rank, however, the number starts from scratch and the percentage is calculated: Divide them by the largest number, for example, 60 60 80 90-> 0 0 1 2-> 0/2 0/2 1/2 2/2 window function: lead (value [, offset [, default]): The data lag (value [, offset [, default]): The first _ Value (value [, boolea]): returns the first value from the ordered result set. If boolea is true, the null value last_value (value [, boolea]) is skipped. return the last value from the ordered result set over: Standard aggregate function (hive 2.1.0) partition by: one or more fields of the basic type order: one or more fields of any type window_clause: window opening in the partition: granularity type :??: In ascending order, star_expr must be smaller than end_expr; otherwise, the whole column value is null or the window range type window (rows | range) between (unbounded | [num]) of the abnormal row type is reported. preceding and ([num] preceding | current row | (unbounded | [num]) following) (rows | range) between current row and (current row | (unbounded | [num]) following) (rows | range) between [num] following and (unbounded | [num]) following -- opens a window in a certain range below the current row [num] To tell the window function the number of records referenced during execution. The num records before or after the current record are included in the statistical range, [u Nbounded] differences between the two windows in which all records before or after the current record are included in the statistical range: range only supports one sorting field, and this field can only be a number or date. If order by is defined, the window clause is missing. The default value is range between unbounded preceding and current row. The window is missing from the start row of the partition to the current row order by and window clause. Default Value: rows between unbounded preceding and unbounded following window: Select name, dept_num, salary, count (*) over (partition by dept_num) as row_cnt, -- sum (salary) over (partition by dept_num order by dept_num) as depttotal, -- sum (salary) over (order by dept_num) as runingtol1, -- sum (salary) over (order by dept_num, name rows unbounded preceding) as runingtotol2 -- Dept _ Num global sorting, name local sorting from employee_contract; Select name, dept_num, salary, count (*) over (partition by dept_num) as row_cnt, sum (salary) over (partition by dept_num order by dept_num) as depttotal, sum (salary) over (order by dept_num) as runingtol1, sum (salary) over (order by dept_num, name rows unbounded preceding) as runingtotol2from employee_contractorder by dept_num, name; <=> independent window clause format: Select name, d Ept_num, salary, count (*) over row_cnt, sum (salary) over depttotal, sum (salary) over runingtol1, sum (salary) over runingtol2from employee_contractorder by dept_num, partition as (partition by dept_num), overdepttotal as (partition by dept_num order by dept_num), partition as (order by dept_num), partition as (order by dept_num, name rows unbounded preceding ); -- Query select name, dept_n by range Um, salary, start_year, max (salary) over (partition by dept_num order by start_year range between 2 preceding and current row) as win1from (Select name, dept_num, salary, year (start_date) as start_yearfrom employee_contract) A; sample 1. random Sampling ?? Select name from employee_iddistribute by rand () sort by rand () limit 5; 2. select * from <buckettable_name> tablesample (bucket <specified Bucket number to sample> out of <total number of buckets> on [colname | rand ()]) table_alias; select name from employee_id_bucketstablesample (bucket 1 out of 2 on rand () A; 3. block sampling select * from <table_name> tablesample (N percent | bytelengthliteral | n rows) s; sample by percentage select name from employee_id_bucketstablesample (20 percent); select name from employee_idtablesample (5 rows) A by row sampling; Select name from employee_idtablesample (700b) A by byte sampling;
Reference

Oracle development: window functions
Languagemanual windowingandanalytics
Percent_rank
Cume_dist (TRANSACT-SQL)
Hive Essentials
Programing hive
Practical hive

Data aggregation and sampling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.