SAP HANA SQL语句UNION 和 UNION ALL的用法

简介:

UNION ALL--不合并重复行

Selects all records from all selectstatements. Duplicates are not removed

UNION [DISTINCT] --合并重复行UNION 和 UNION DISTINCT功能相同

Selects all unique records from all selectstatements by removing duplicates found from different select statements. UNION has the same function as UNION DISTINCT.

合并重复行

select * from A union select * from B 

不合并重复行 select * from A union all select * from B 

按某个字段排序 --合并重复行

select * from ( select * from A union select * from B) AS T order by 字段名

不合并重复行

select * from ( select * from A union all select * from B) AS T order by 字段名

create column table tunion_1( id int primary key, customer varchar(5), year int, product varchar(5), sales int );

create column table tunion_2 ( id int primary key, customer varchar(5), year int, product varchar(5), sales int );


insert into tunion_1values(1, 'C1', 2009, 'P1', 100); 
insert into tunion_1values(2, 'C1', 2009, 'P2', 200); 
insert into tunion_1values(3, 'C1', 2010, 'P1', 50); 
insert into tunion_1values(4, 'C1', 2010, 'P2', 150); 
insert into tunion_1values(5, 'C2', 2009, 'P1', 200); 
insert into tunion_1values(6, 'C2', 2009, 'P2', 300); 
insert into tunion_1values(7, 'C2', 2010, 'P1', 100); 
insert into tunion_1values(8, 'C2', 2010, 'P2', 150); 

  insert into tunion_2 values(1, 'C1', 2011, 'P1', 100); 
insert into tunion_2 values(2, 'C1', 2011, 'P2', 200); 
insert into tunion_2 values(3, 'C1', 2011, 'P1', 50); 
insert into tunion_2 values(4, 'C1', 2011, 'P2', 150); 
insert into tunion_2 values(5, 'C2', 2011, 'P1', 200); 
insert into tunion_2 values(6, 'C2', 2011, 'P2', 300); 
insert into tunion_2 values(7, 'C2', 2011, 'P1', 100); 
insert into tunion_2 values(8, 'C2', 2011, 'P2', 150); 
insert into tunion_2 values(9, 'C1', 2011, 'P1', 100);

select count(1) from (select  customer,year,product,sales from tunion_1 union select  customer,year,product,sales from tunion_2)    结果--->> 16

image

select count(1) from (select  customer,year,product,sales from tunion_1 UNION DISTINCT select  customer,year,product,sales from tunion_2)  结果 --->> 16

image

select count(1) from (select  customer,year,product,sales from tunion_1 union all select  customer,year,product,sales from tunion_2)  结果—>>17

image

select  * from (select  customer,year,product,sales from tunion_1 union all select  customer,year,product,sales from tunion_2) order by customer

image

select  * from (select  customer,year,product,sales from tunion_1 union select  customer,year,product,sales from tunion_2) order by customer

image

专注于企业信息化,最近对股票数据分析较为感兴趣,可免费分享股票个股主力资金实时变化趋势分析工具,股票交流QQ群:457394862
分类:  SAP HANA

本文转自沧海-重庆博客园博客,原文链接:http://www.cnblogs.com/omygod/archive/2013/04/09/3010484.html,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
开发者 数据处理 数据管理
SAP HANA 的不同类型
SAP HANA 的不同类型
38 9
SAP HANA 的不同类型
|
1月前
|
SQL 数据库 索引
关于 SAP ABAP REPOSRC 数据库表在 HANA 中的 DDL Definition
关于 SAP ABAP REPOSRC 数据库表在 HANA 中的 DDL Definition
20 1
关于 SAP ABAP REPOSRC 数据库表在 HANA 中的 DDL Definition
|
1月前
|
数据库 存储 监控
什么是 SAP HANA 内存数据库 的 Delta Storage
什么是 SAP HANA 内存数据库 的 Delta Storage
18 0
什么是 SAP HANA 内存数据库 的 Delta Storage
|
4天前
|
SQL 关系型数据库 MySQL
MySQL SQL error: #1271 - Illegal mix of collations for operation ‘UNION‘
MySQL SQL error: #1271 - Illegal mix of collations for operation ‘UNION‘
|
24天前
|
SQL Web App开发 安全
CTF-Web安全--SQL注入之Union注入详解
CTF-Web安全--SQL注入之Union注入详解
|
27天前
|
Java BI API
SAP Cloud for Customer 里如何通过 ABSL 二次开发方式消费 SAP S/4HANA 系统的 API
SAP Cloud for Customer 里如何通过 ABSL 二次开发方式消费 SAP S/4HANA 系统的 API
18 0
|
1月前
|
Linux 数据库连接 数据库
如何在 Linux 系统下使用 PHP 和 ODBC 连接 SAP HANA
如何在 Linux 系统下使用 PHP 和 ODBC 连接 SAP HANA
17 0
|
1月前
|
数据库
SAP S/4HANA 系统的底层基石 - 通过实际的例子,介绍 CDS View 入门级的概念试读版
SAP S/4HANA 系统的底层基石 - 通过实际的例子,介绍 CDS View 入门级的概念试读版
8 0
SAP S/4HANA 系统的底层基石 - 通过实际的例子,介绍 CDS View 入门级的概念试读版
|
1月前
|
数据库 SQL 应用服务中间件
SAP ABAP CDS View 和 HANA CDS View 相同点和不同点辨析
SAP ABAP CDS View 和 HANA CDS View 相同点和不同点辨析
22 0
SAP ABAP CDS View 和 HANA CDS View 相同点和不同点辨析
|
2月前
|
SQL Java 关系型数据库
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
MyBatis的动态SQL之OGNL(Object-Graph Navigation Language)表达式以及各种标签的用法
18 0

热门文章

最新文章