zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    jQuery 简单滑动轮播图效果
    西工大:同学你好,回来挂科!
    【入门】产品经理零基础怎么入门?
    【考点】 HashMap,HashTable,CurrentHashMap,LinkedHashMap,TreeMap简述
    P图鬼才们集体上线!高校毕业照P图哪家强?
    【实战】怎样实现前端裁剪上传图片功能
    校招选产品经理岗?给你浇盆水
    战胜70%对手的校招开发岗简历是这个样子的
    两个人遇到熊,装死的和转身跑的,哪个能活下来
    第一份实习工作,我应该学到什么?
  • 原文地址:https://www.cnblogs.com/omygod/p/3010484.html
Copyright © 2011-2022 走看看