table of Contents
This blog to talk about the SELECT statement GROUP BY clause reinforced polymer properties.
Analog data (separator is '\ t')
上海市 浦东新区 2019-01-02 2019-01-02 8:50:32 20
上海市 浦东新区 2019-01-02 2019-01-02 9:50:32 60
上海市 浦东新区 2019-01-02 2019-01-02 10:50:32 55
上海市 浦东新区 2019-01-02 2019-01-02 11:50:32 70
上海市 浦东新区 2019-01-02 2019-01-02 13:50:32 80
上海市 浦东新区 2019-01-02 2019-01-02 15:50:32 100
上海市 浦东新区 2019-01-02 2019-01-02 16:50:32 90
上海市 浦东新区 2019-01-02 2019-01-02 17:50:32 95
上海市 浦东新区 2019-01-02 2019-01-02 18:50:32 55
上海市 浦东新区 2019-01-03 2019-01-02 8:50:32 23
上海市 浦东新区 2019-01-03 2019-01-02 9:50:32 63
上海市 浦东新区 2019-01-03 2019-01-02 10:50:32 53
上海市 浦东新区 2019-01-03 2019-01-02 11:50:32 73
上海市 浦东新区 2019-01-03 2019-01-02 13:50:32 83
上海市 浦东新区 2019-01-03 2019-01-02 15:50:32 130
上海市 浦东新区 2019-01-03 2019-01-02 16:50:32 93
上海市 浦东新区 2019-01-03 2019-01-02 17:50:32 93
上海市 浦东新区 2019-01-03 2019-01-02 18:50:32 53
上海市 普陀区 2019-01-02 2019-01-02 8:50:32 26
上海市 普陀区 2019-01-02 2019-01-02 9:50:32 66
上海市 普陀区 2019-01-02 2019-01-02 10:50:32 56
上海市 普陀区 2019-01-02 2019-01-02 11:50:32 76
上海市 普陀区 2019-01-02 2019-01-02 13:50:32 86
上海市 普陀区 2019-01-02 2019-01-02 15:50:32 120
上海市 普陀区 2019-01-02 2019-01-02 16:50:32 96
上海市 普陀区 2019-01-02 2019-01-02 17:50:32 96
上海市 普陀区 2019-01-02 2019-01-02 18:50:32 56
上海市 普陀区 2019-01-03 2019-01-02 8:50:32 28
上海市 普陀区 2019-01-03 2019-01-02 9:50:32 66
上海市 普陀区 2019-01-03 2019-01-02 10:50:32 56
上海市 普陀区 2019-01-03 2019-01-02 11:50:32 76
上海市 普陀区 2019-01-03 2019-01-02 13:50:32 86
上海市 普陀区 2019-01-03 2019-01-02 15:50:32 131
上海市 普陀区 2019-01-03 2019-01-02 16:50:32 96
上海市 普陀区 2019-01-03 2019-01-02 17:50:32 96
上海市 普陀区 2019-01-03 2019-01-02 18:50:32 56
上海市 嘉定区 2019-01-02 2019-01-02 8:50:32 28
上海市 嘉定区 2019-01-02 2019-01-02 9:50:32 68
上海市 嘉定区 2019-01-02 2019-01-02 10:50:32 58
上海市 嘉定区 2019-01-02 2019-01-02 11:50:32 78
上海市 嘉定区 2019-01-02 2019-01-02 13:50:32 88
上海市 嘉定区 2019-01-02 2019-01-02 15:50:32 139
上海市 嘉定区 2019-01-02 2019-01-02 16:50:32 98
上海市 嘉定区 2019-01-02 2019-01-02 17:50:32 98
上海市 嘉定区 2019-01-02 2019-01-02 18:50:32 58
上海市 嘉定区 2019-01-03 2019-01-02 8:50:32 35
上海市 嘉定区 2019-01-03 2019-01-02 9:50:32 68
上海市 嘉定区 2019-01-03 2019-01-02 10:50:32 58
上海市 嘉定区 2019-01-03 2019-01-02 11:50:32 78
上海市 嘉定区 2019-01-03 2019-01-02 13:50:32 88
上海市 嘉定区 2019-01-03 2019-01-02 15:50:32 145
上海市 嘉定区 2019-01-03 2019-01-02 16:50:32 98
上海市 嘉定区 2019-01-03 2019-01-02 17:50:32 98
上海市 嘉定区 2019-01-03 2019-01-02 18:50:32 53
广东省 深圳市 2019-01-02 2019-01-02 8:50:32 29
广东省 深圳市 2019-01-02 2019-01-02 9:50:32 69
广东省 深圳市 2019-01-02 2019-01-02 10:50:32 59
广东省 深圳市 2019-01-02 2019-01-02 11:50:32 79
广东省 深圳市 2019-01-02 2019-01-02 13:50:32 89
广东省 深圳市 2019-01-02 2019-01-02 15:50:32 188
广东省 深圳市 2019-01-02 2019-01-02 16:50:32 99
广东省 深圳市 2019-01-02 2019-01-02 17:50:32 99
广东省 深圳市 2019-01-02 2019-01-02 18:50:32 59
广东省 深圳市 2019-01-03 2019-01-02 8:50:32 29
广东省 深圳市 2019-01-03 2019-01-02 9:50:32 69
广东省 深圳市 2019-01-03 2019-01-02 10:50:32 59
广东省 深圳市 2019-01-03 2019-01-02 11:50:32 79
广东省 深圳市 2019-01-03 2019-01-02 13:50:32 89
广东省 深圳市 2019-01-03 2019-01-02 15:50:32 166
广东省 深圳市 2019-01-03 2019-01-02 16:50:32 99
广东省 深圳市 2019-01-03 2019-01-02 17:50:32 99
广东省 深圳市 2019-01-03 2019-01-02 18:50:32 59
广东省 广州市 2019-01-02 2019-01-02 8:50:32 34
广东省 广州市 2019-01-02 2019-01-02 9:50:32 65
广东省 广州市 2019-01-02 2019-01-02 10:50:32 59
广东省 广州市 2019-01-02 2019-01-02 11:50:32 67
广东省 广州市 2019-01-02 2019-01-02 13:50:32 89
广东省 广州市 2019-01-02 2019-01-02 15:50:32 155
广东省 广州市 2019-01-02 2019-01-02 16:50:32 99
广东省 广州市 2019-01-02 2019-01-02 17:50:32 99
广东省 广州市 2019-01-02 2019-01-02 18:50:32 59
广东省 广州市 2019-01-03 2019-01-02 8:50:32 29
广东省 广州市 2019-01-03 2019-01-02 9:50:32 69
广东省 广州市 2019-01-03 2019-01-02 10:50:32 59
广东省 广州市 2019-01-03 2019-01-02 11:50:32 79
广东省 广州市 2019-01-03 2019-01-02 13:50:32 89
广东省 广州市 2019-01-03 2019-01-02 15:50:32 133
广东省 广州市 2019-01-03 2019-01-02 16:50:32 88
广东省 广州市 2019-01-03 2019-01-02 17:50:32 99
广东省 广州市 2019-01-03 2019-01-02 18:50:32 67
广东省 珠海市 2019-01-02 2019-01-02 8:50:32 29
广东省 珠海市 2019-01-02 2019-01-02 9:50:32 70
广东省 珠海市 2019-01-02 2019-01-02 10:50:32 59
广东省 珠海市 2019-01-02 2019-01-02 11:50:32 79
广东省 珠海市 2019-01-02 2019-01-02 13:50:32 88
广东省 珠海市 2019-01-02 2019-01-02 15:50:32 126
广东省 珠海市 2019-01-02 2019-01-02 16:50:32 99
广东省 珠海市 2019-01-02 2019-01-02 17:50:32 99
广东省 珠海市 2019-01-02 2019-01-02 18:50:32 59
广东省 珠海市 2019-01-03 2019-01-02 8:50:32 29
广东省 珠海市 2019-01-03 2019-01-02 9:50:32 69
广东省 珠海市 2019-01-03 2019-01-02 10:50:32 59
广东省 珠海市 2019-01-03 2019-01-02 11:50:32 69
广东省 珠海市 2019-01-03 2019-01-02 13:50:32 79
广东省 珠海市 2019-01-03 2019-01-02 15:50:32 133
广东省 珠海市 2019-01-03 2019-01-02 16:50:32 99
广东省 珠海市 2019-01-03 2019-01-02 17:50:32 85
广东省 珠海市 2019-01-03 2019-01-02 18:50:32 59
Creating tables;
create table xinzeng_tb (
shengfen string,
dishi string,
riqi string,
shijian string,
xinzeng int
)
row format delimited fields terminated by '\t'
location 'hdfs://bigdata-training01.fuyun.com:8020/user/hive/warehouse-3.1.1/db_window.db'
Download Data:
load data local inpath '/opt/datas/xinzeng_tb' into table xinzeng_tb
GROUPING SETS
grouping sets corresponding to a plurality of logical group by group statistics after the union
By province and city statistics added
select
shengfen,
dishi,
sum(xinzeng) as sum_xinzeng
from xinzeng_tb
group by shengfen, dishi
grouping sets (shengfen, dishi)
-- cluster by sum_xinzeng
distribute by sum_xinzeng sort by sum_xinzeng desc;
OK
shengfen dishi sum_xinzeng
广东省 NULL 4345
上海市 NULL 4092
NULL 深圳市 1518
NULL 广州市 1438
NULL 嘉定区 1434
NULL 珠海市 1389
NULL 普陀区 1369
NULL 浦东新区 1289
Time taken: 83.518 seconds, Fetched: 8 row(s)
Equivalent to:
select
shengfen,
null,
sum(xinzeng) as sum_xinzeng
from xinzeng_tb
group by shengfen
union all
select
null,
dishi,
sum(xinzeng) as sum_xinzeng
from xinzeng_tb
group by dishi
distribute by sum_xinzeng sort by sum_xinzeng desc;
OK
_u2.shengfen _u2._c1 _u2.sum_xinzeng
广东省 NULL 4345
上海市 NULL 4092
NULL 深圳市 1518
NULL 广州市 1438
NULL 嘉定区 1434
NULL 珠海市 1389
NULL 普陀区 1369
NULL 浦东新区 1289
Time taken: 124.22 seconds, Fetched: 8 row(s)
The above method according to provinces and cities that packet as null, can be optimized for:
select
shengfen,
dishi,
sum(xinzeng) as sum_xinzeng
from xinzeng_tb
group by shengfen, dishi
grouping sets (shengfen, (shengfen, dishi))
-- cluster by sum_xinzeng
distribute by sum_xinzeng sort by sum_xinzeng desc;
OK
shengfen dishi sum_xinzeng
广东省 NULL 4345
上海市 NULL 4092
广东省 深圳市 1518
广东省 广州市 1438
上海市 嘉定区 1434
广东省 珠海市 1389
上海市 普陀区 1369
上海市 浦东新区 1289
Time taken: 77.62 seconds, Fetched: 8 row(s)
Equivalent to:
select
shengfen,
null,
sum(xinzeng) as sum_xinzeng
from xinzeng_tb
group by shengfen
union all
select
shengfen,
dishi,
sum(xinzeng) as sum_xinzeng
from xinzeng_tb
group by shengfen, dishi
distribute by sum_xinzeng sort by sum_xinzeng desc;
OK
_u2.shengfen _u2._c1 _u2.sum_xinzeng
广东省 NULL 4345
上海市 NULL 4092
广东省 深圳市 1518
广东省 广州市 1438
上海市 嘉定区 1434
广东省 珠海市 1389
上海市 普陀区 1369
上海市 浦东新区 1289
Time taken: 128.156 seconds, Fetched: 8 row(s)
By grouping sets group by query and query equivalence relations to the union
grouping sets statements | group by statement |
---|---|
SELECT a, b, SUM© FROM tab1 GROUP BY a, b GROUPING SETS ( (a,b) ) | SELECT a, b, SUM© FROM tab1 GROUP BY a, b |
SELECT a, b, SUM( c ) FROM tab1 GROUP BY a, b GROUPING SETS ( (a,b), a) | SELECT a, b, SUM( c ) FROM tab1 GROUP BY a, b UNION SELECT a, null, SUM( c ) FROM tab1 GROUP BY a |
SELECT a,b, SUM( c ) FROM tab1 GROUP BY a, b GROUPING SETS (a,b) | SELECT a, null, SUM( c ) FROM tab1 GROUP BY a UNION SELECT null, b, SUM( c ) FROM tab1 GROUP BY b |
SELECT a, b, SUM( c ) FROM tab1 GROUP BY a, b GROUPING SETS ( (a, b), a, b, ( ) ) | SELECT a, b, SUM( c ) FROM tab1 GROUP BY a, b UNION SELECT a, null, SUM( c ) FROM tab1 GROUP BY a, null UNION SELECT null, b, SUM( c ) FROM tab1 GROUP BY null, b UNION SELECT null, null, SUM( c ) FROM tab1 |
Grouping__ID (two underscores _)
This function returns a bit vector, the bit vector corresponding to each column if present. For each column, a row of the result set if already polymerized the column in the result set of a row is "1", otherwise the value is "0." This free time can be used to distinguish the data values.
Example 1:
select
shengfen,
dishi,
sum(xinzeng) as sum_xinzeng,
grouping__id
from xinzeng_tb
group by shengfen, dishi
grouping sets (shengfen, (shengfen, dishi))
-- cluster by grouping__id
distribute by grouping__id sort by grouping__id, sum_xinzeng desc;
OK
shengfen dishi sum_xinzeng grouping__id
广东省 深圳市 1518 0
广东省 广州市 1438 0
上海市 嘉定区 1434 0
广东省 珠海市 1389 0
上海市 普陀区 1369 0
上海市 浦东新区 1289 0
广东省 NULL 4345 1
上海市 NULL 4092 1
Time taken: 74.346 seconds, Fetched: 8 row(s)
Equivalent to:
select
shengfen,
null,
sum(xinzeng) as sum_xinzeng,
1 as grouping__id
from xinzeng_tb
group by shengfen
union all
select
shengfen,
dishi,
sum(xinzeng) as sum_xinzeng,
0 as grouping__id
from xinzeng_tb
group by shengfen, dishi
distribute by grouping__id sort by grouping__id, sum_xinzeng desc;
OK
_u2.shengfen _u2._c1 _u2.sum_xinzeng _u2.grouping__id
广东省 深圳市 1518 0
广东省 广州市 1438 0
上海市 嘉定区 1434 0
广东省 珠海市 1389 0
上海市 普陀区 1369 0
上海市 浦东新区 1289 0
广东省 NULL 4345 1
上海市 NULL 4092 1
Time taken: 117.174 seconds, Fetched: 8 row(s)
Example 2:
select
shengfen,
dishi,
sum(xinzeng) as sum_xinzeng,
grouping__id
from xinzeng_tb
group by shengfen, dishi
with rollup
-- cluster by grouping__id
distribute by grouping__id sort by grouping__id, sum_xinzeng desc;
OK
shengfen dishi sum_xinzeng grouping__id
广东省 深圳市 1518 0
广东省 广州市 1438 0
上海市 嘉定区 1434 0
广东省 珠海市 1389 0
上海市 普陀区 1369 0
上海市 浦东新区 1289 0
广东省 NULL 4345 1
上海市 NULL 4092 1
NULL NULL 8437 3
Time taken: 69.779 seconds, Fetched: 9 row(s)
Example 2sql explained statement:
group_id is to distinguish each output result is a group by which the data belongs. It is a bit combination of the current data based on the group by group by the order of the fields following declaration exists.
grouping__id 0 is the group by all the columns are selected, binary 00, it is identified as 0
grouping__id 1 is a group by only one was selected, binary 01, it is identified as 1
grouping__id 3 is a group by no one is selected, the binary 11, it is identified as a 3
Grouping
Whether the packet function indicates the GROUP BY clause expressions given row polymerization. A value of 0 indicates a packet belonging to the columns, column 1 indicates the value of the packet does not belong to the set.
select
shengfen,
dishi,
sum(xinzeng) as sum_xinzeng,
grouping(shengfen, dishi) grouping_two,
grouping(shengfen) grouping_s,
grouping(dishi) grouping_d,
grouping__id
from xinzeng_tb
group by shengfen, dishi
with rollup
-- cluster by grouping__id
distribute by grouping__id sort by grouping__id, sum_xinzeng desc;
OK
shengfen dishi sum_xinzeng grouping_two grouping_s grouping_d grouping__id
广东省 深圳市 1518 0 0 0 0
广东省 广州市 1438 0 0 0 0
上海市 嘉定区 1434 0 0 0 0
广东省 珠海市 1389 0 0 0 0
上海市 普陀区 1369 0 0 0 0
上海市 浦东新区 1289 0 0 0 0
广东省 NULL 4345 1 0 1 1
上海市 NULL 4092 1 0 1 1
NULL NULL 8437 3 1 1 3
Time taken: 73.224 seconds, Fetched: 9 row(s)
cube and rollup
CUBE / ROLLUP must be used with GROUP BY.
cube can get answers to all possible questions on these dimensions aggregation group by.
For example:
the GROUP BY A, B, C is equivalent to the WITH CUBE
GROUP BY a, b, c GROUPING SETS ((a, b, c), (a, b), (b, c), (a, c), (a), (b), ©, ()).
Rollups
the ROLLUP clause in the polymerization in the dimension hierarchy level with GROUP BY.
Using ROLLUP to a, b, c packet, assuming the hierarchy is "a" drill down "b", to drill "c".
GROUP BY a, b, c, WITH ROLLUP equivalent GROUP BY a, b, c GROUPING SETS ((a, b, c), (a, b), (a), ()).
rollup
select
shengfen,
dishi,
riqi,
sum(xinzeng) as sum_xinzeng,
grouping__id
from xinzeng_tb
group by shengfen, dishi, riqi
--grouping sets (shengfen, dishi, riqi)
with rollup
--cluster by grouping__id
distribute by grouping__id sort by grouping__id desc, sum_xinzeng desc;
OK
shengfen dishi riqi sum_xinzeng grouping__id
NULL NULL NULL 8437 7
广东省 NULL NULL 4345 3
上海市 NULL NULL 4092 3
广东省 深圳市 NULL 1518 1
广东省 广州市 NULL 1438 1
上海市 嘉定区 NULL 1434 1
广东省 珠海市 NULL 1389 1
上海市 普陀区 NULL 1369 1
上海市 浦东新区 NULL 1289 1
广东省 深圳市 2019-01-02 770 0
广东省 深圳市 2019-01-03 748 0
广东省 广州市 2019-01-02 726 0
上海市 嘉定区 2019-01-03 721 0
上海市 嘉定区 2019-01-02 713 0
广东省 广州市 2019-01-03 712 0
广东省 珠海市 2019-01-02 708 0
上海市 普陀区 2019-01-03 691 0
广东省 珠海市 2019-01-03 681 0
上海市 普陀区 2019-01-02 678 0
上海市 浦东新区 2019-01-03 664 0
上海市 浦东新区 2019-01-02 625 0
Time taken: 75.995 seconds, Fetched: 21 row(s)
cube
select
shengfen,
dishi,
riqi,
sum(xinzeng) as sum_xinzeng,
grouping__id
from xinzeng_tb
group by shengfen, dishi, riqi
--grouping sets (shengfen, dishi, riqi)
with cube
--cluster by grouping__id
distribute by grouping__id sort by grouping__id desc, sum_xinzeng desc;
OK
shengfen dishi riqi sum_xinzeng grouping__id
NULL NULL NULL 8437 7
NULL NULL 2019-01-02 4220 6
NULL NULL 2019-01-03 4217 6
NULL 深圳市 NULL 1518 5
NULL 广州市 NULL 1438 5
NULL 嘉定区 NULL 1434 5
NULL 珠海市 NULL 1389 5
NULL 普陀区 NULL 1369 5
NULL 浦东新区 NULL 1289 5
NULL 深圳市 2019-01-02 770 4
NULL 深圳市 2019-01-03 748 4
NULL 广州市 2019-01-02 726 4
NULL 嘉定区 2019-01-03 721 4
NULL 嘉定区 2019-01-02 713 4
NULL 广州市 2019-01-03 712 4
NULL 珠海市 2019-01-02 708 4
NULL 普陀区 2019-01-03 691 4
NULL 珠海市 2019-01-03 681 4
NULL 普陀区 2019-01-02 678 4
NULL 浦东新区 2019-01-03 664 4
NULL 浦东新区 2019-01-02 625 4
广东省 NULL NULL 4345 3
上海市 NULL NULL 4092 3
广东省 NULL 2019-01-02 2204 2
广东省 NULL 2019-01-03 2141 2
上海市 NULL 2019-01-03 2076 2
上海市 NULL 2019-01-02 2016 2
广东省 深圳市 NULL 1518 1
广东省 广州市 NULL 1438 1
上海市 嘉定区 NULL 1434 1
广东省 珠海市 NULL 1389 1
上海市 普陀区 NULL 1369 1
上海市 浦东新区 NULL 1289 1
广东省 深圳市 2019-01-02 770 0
广东省 深圳市 2019-01-03 748 0
广东省 广州市 2019-01-02 726 0
上海市 嘉定区 2019-01-03 721 0
上海市 嘉定区 2019-01-02 713 0
广东省 广州市 2019-01-03 712 0
广东省 珠海市 2019-01-02 708 0
上海市 普陀区 2019-01-03 691 0
广东省 珠海市 2019-01-03 681 0
上海市 普陀区 2019-01-02 678 0
上海市 浦东新区 2019-01-03 664 0
上海市 浦东新区 2019-01-02 625 0
Time taken: 72.189 seconds, Fetched: 45 row(s)
hive.new.job.grouping.set.cardinality quantity for each data replication
These are enhancements group by, and is more convenient to write logical group by writing a SQL statement.
set hive.new.job.grouping.set.cardinality = 32, the significance of this arrangement is that the interpreter informed, before group by, each of the data copy amount of 32 parts or less.
select the fields are full of A, B, C, but we know that due to the group by the presence, select the field should not have appeared non-group by fields, so here we pay special instructions, if the interpreter found group by A, C However select a, B, C will then run all the results taken from table copy, B are set to null, the result is, B are null.
The official document: https://cwiki.apache.org/confluence/display/Hive/Enhanced+Aggregation,+Cube,+Grouping+and+Rollup