全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网 钉钉智能硬件
流计算

UNION ALL语句

更新时间:2017-11-23 18:24:14

语法格式

  1. select_statement
  2. UNION ALL
  3. select_statement;

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

特别注意的是: 当前流计算不支持UNION。UNION ALL 是允许重复值,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
本文导读目录