UNION ALL语句

简介:

语法

 
  
  1. select_statement
  2. UNION ALL
  3. select_statement;

UNION ALL 语句将两个流合并。要求两个流的字段完全一致,包括字段类型、字段顺序。

注意: 实时计算同样支持 UNION 函数。UNION ALL 允许重复值,UNION 不允许重复值。在Flink 底层,UNION 是 UNION ALL + Distinct , 因此 UNION 运行效率比较低,一般不推荐使用 UNION。

示例一

 
  
  1. SELECT *
  2. FROM (
  3. (SELECT user FROM 表名 WHERE a % 2 = 0)
  4. UNION ALL
  5. (SELECT user FROM 表名 WHERE b = 0)
  6. );

示例二

测试数据

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

测试语句

 
  
  1. create table test_source_union1 (
  2. a varchar,
  3. b bigint,
  4. c bigint
  5. ) WITH (
  6. type='datahub',
  7. endpoint='',
  8. accessId='',
  9. accessKey='',
  10. projectName='',
  11. topic='',
  12. project=''
  13. );
  14. create table test_source_union2 (
  15. a varchar,
  16. b bigint,
  17. c bigint
  18. ) WITH (
  19. type='datahub',
  20. endpoint='',
  21. accessId='',
  22. accessKey='',
  23. projectName='',
  24. topic='',
  25. project=''
  26. );
  27. create table test_source_union3 (
  28. a varchar,
  29. b bigint,
  30. c bigint
  31. ) WITH (
  32. type='datahub',
  33. endpoint='',
  34. accessId='',
  35. accessKey='',
  36. projectName='',
  37. topic='',
  38. project=''
  39. );
  40. create table test_result_union (
  41. d VARCHAR,
  42. e BIGINT,
  43. f BIGINT,
  44. primary key(d)
  45. ) WITH (
  46. type='rds',
  47. url='',
  48. username='',
  49. password='',
  50. tableName=''
  51. );
  52. INSERT into test_result_union
  53. SELECT
  54. a,
  55. sum(b),
  56. sum(c)
  57. FROM
  58. (SELECT * from test_source_union1
  59. UNION ALL
  60. SELECT * from test_source_union2
  61. UNION ALL
  62. SELECT * from test_source_union3
  63. )t
  64. 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语句
相关文章
|
5月前
|
SQL 关系型数据库 MySQL
第3章_基本select语句
第3章_基本select语句
44 0
|
6月前
可以使用 UNION 或者 UNION ALL 来合并多个 SELECT 语句的结果
可以使用 UNION 或者 UNION ALL 来合并多个 SELECT 语句的结果
66 7
|
8月前
union和union all 的区别
union和union all 的区别
81 0
|
10月前
|
SQL
|
10月前
|
SQL 存储 关系型数据库
03_基本的SELECT语句
03_基本的SELECT语句
64 0
|
10月前
|
SQL 存储 前端开发
第03章_基本的SELECT语句
第03章_基本的SELECT语句
200 1
|
11月前
|
SQL
SQL语句中union all和union的区别以及用法
SQL语句中union all和union的区别以及用法
100 0
SQL:union all和union的区别 和使用
SQL:union all和union的区别 和使用
2261 0