语法
select_statement
UNION ALL
select_statement;
UNION ALL 语句将两个流合并。要求两个流的字段完全一致,包括字段类型、字段顺序。
注意: 实时计算同样支持 UNION 函数。UNION ALL 允许重复值,UNION 不允许重复值。在Flink 底层,UNION 是 UNION ALL + Distinct , 因此 UNION 运行效率比较低,一般不推荐使用 UNION。
示例一
SELECT *
FROM (
(SELECT user FROM 表名 WHERE a % 2 = 0)
UNION ALL
(SELECT user FROM 表名 WHERE b = 0)
);
示例二
测试数据
test_source_union1:
a(varchar) | b(bigint) | c(bigint) |
---|---|---|
test1 | 1 | 10 |
test_source_union2:
a(varchar) | b(bigint) | c(bigint) |
---|---|---|
test1 | 1 | 10 |
test2 | 2 | 20 |
test_source_union3:
a(varchar) | b(bigint) | c(bigint) |
---|---|---|
test1 | 1 | 10 |
test2 | 2 | 20 |
test1 | 1 | 10 |
测试语句
create table test_source_union1 (
a varchar,
b bigint,
c bigint
) WITH (
type='datahub',
endpoint='',
accessId='',
accessKey='',
projectName='',
topic='',
project=''
);
create table test_source_union2 (
a varchar,
b bigint,
c bigint
) WITH (
type='datahub',
endpoint='',
accessId='',
accessKey='',
projectName='',
topic='',
project=''
);
create table test_source_union3 (
a varchar,
b bigint,
c bigint
) WITH (
type='datahub',
endpoint='',
accessId='',
accessKey='',
projectName='',
topic='',
project=''
);
create table test_result_union (
d VARCHAR,
e BIGINT,
f BIGINT,
primary key(d)
) WITH (
type='rds',
url='',
username='',
password='',
tableName=''
);
INSERT into test_result_union
SELECT
a,
sum(b),
sum(c)
FROM
(SELECT * from test_source_union1
UNION ALL
SELECT * from test_source_union2
UNION ALL
SELECT * from test_source_union3
)t
GROUP BY a;
测试结果
d(varchar) | e(bigint) | f(bigint) |
---|---|---|
test1 | 1 | 10 |
test2 | 2 | 20 |
test1 | 2 | 20 |
test1 | 3 | 30 |
test2 | 4 | 40 |
test1 | 4 | 40 |
本文转自实时计算——
UNION ALL语句