1. 程式人生 > >union不支持orderByClause、clusterByClause、distributeByClause、sortByClause或limitClause

union不支持orderByClause、clusterByClause、distributeByClause、sortByClause或limitClause

war mic tab failed union all des exec sid 代碼

union all

union

相同點 是 相當於上下拼接 上下兩個拼接表必須字段保持一致

不同 union有去重效果,速度會更慢。

=============================================================================================================================

union all的子句裏不支持orderByClause、clusterByClause、distributeByClause、sortByClause或limitClause
解決:改造hql,去掉union all子查詢裏的orderByClause、clusterByClause、distributeByClause、sortByClause和limitClause語句
示例:select t.id from (select dummy from default.dual limit 1 union all select dummy from default.dual limit 1)t;

去掉兩個子查詢裏的limit 1;

create table lk3 (id string,nname string,grade int,goldUser int);

insert into lk3 values
(1,jack,300, 10 ),
(2,mach, 200, 10 ),
(3,lich, 100 ,10 ),
(4,rock, 1, 0 ),
(5,mick, 1 ,10 ),
(6,kight, 0 ,10 ),
(7,babaya, 0, 0 ),
(8,kano, 0, 10);
 select * from lk3;
+
---------+------------+------------+---------------+--+ | lk3.id | lk3.nname | lk3.grade | lk3.golduser | +---------+------------+------------+---------------+--+ | 1 | jack | 300 | 10 | | 2 | mach | 200 | 10 | | 3 | lich | 100 |
10 | | 4 | rock | 1 | 0 | | 5 | mick | 1 | 10 | | 6 | kight | 0 | 10 | | 7 | babaya | 0 | 0 | | 8 | kano | 0 | 10 | +---------+------------+------------+---------------+--+

報錯代碼:

0: jdbc:hive2://localhost:10000> SELECT * FROM lk3 where grade != 1 order by grade desc,goldUser
. . . . . . . . . . . . . . . .> union all
. . . . . . . . . . . . . . . .> select * from lk3 where grade != 1 order by grade desc,goldUser;
Error: Error while compiling statement: FAILED: ParseException line 3:63 Failed to recognize predicate <EOF>.
Failed rule: orderByClause clusterByClause distributeByClause sortByClause limitClause can only be applied to the whole union. in statement (state=42000,code=40000)

正確處理

0: jdbc:hive2://localhost:10000> select * from (SELECT * FROM lk3 where grade != 1 order by grade desc,goldUser limit 1) a
. . . . . . . . . . . . . . . .> union all
. . . . . . . . . . . . . . . .> select * from lk3 where grade != 1 order by grade desc,goldUser limit 2,3;
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
+--------+-----------+-----------+--------------+--+
| u1.id  | u1.nname  | u1.grade  | u1.golduser  |
+--------+-----------+-----------+--------------+--+
| 2      | mach      | 200       | 10           |
| 3      | lich      | 100       | 10           |
| 7      | babaya    | 0         | 0            |
+--------+-----------+-----------+--------------+--+
3 rows selected (33.439 seconds)

union不支持orderByClause、clusterByClause、distributeByClause、sortByClause或limitClause