zoukankan      html  css  js  c++  java
  • Union

    http://www.runoob.com/sql/sql-union.html

    使用sql server数据库

    构造数据

    DECLARE @sql NVARCHAR(4000);
    IF OBJECT_ID('UnionTest1') IS NOT NULL
        BEGIN
            PRINT 'Table UnionTest1 exist, will drop and create';
            SET @sql = 'DROP TABLE UnionTest1';
            EXEC sys.sp_executesql @sql;
        END;
    ELSE
        PRINT 'Table UnionTest1 not exist, will create';
    
    IF OBJECT_ID('UnionTest2') IS NOT NULL
        BEGIN
            PRINT 'Table UnionTest2 exist, will drop and create';
            SET @sql = 'DROP TABLE UnionTest2';
            EXEC sys.sp_executesql @sql;
        END;
    ELSE
        PRINT 'Table UnionTest2 not exist, will create';
    
    
    CREATE TABLE UnionTest1
        (
            rut NVARCHAR(4000) NOT NULL ,
            ProductCode NVARCHAR(4000) NOT NULL ,
            ProductGroupCode NVARCHAR(4000) NULL
                DEFAULT NULL ,
            [Count] INT NOT NULL
        );
    
    CREATE TABLE UnionTest2
        (
            rut NVARCHAR(4000) NOT NULL ,
            ProductCode NVARCHAR(4000) NULL
                DEFAULT NULL ,
            ProductGroupCode NVARCHAR(4000) NOT NULL ,
            [Count] INT NOT NULL
        );
    
    INSERT INTO dbo.UnionTest1 ( rut ,
                                 ProductCode ,
                                 [Count] )
    VALUES ( N'001' ,      -- rut - nvarchar(4000)
             N'Product1' , -- ProductCode - nvarchar(4000)
             1             -- Count - int
        );
    INSERT INTO dbo.UnionTest1 ( rut ,
                                 ProductCode ,
                                 [Count] )
    VALUES ( N'001' ,      -- rut - nvarchar(4000)
             N'Product3' , -- ProductCode - nvarchar(4000)
             3             -- Count - int
        );
    INSERT INTO dbo.UnionTest1 ( rut ,
                                 ProductCode ,
                                 [Count] )
    VALUES ( N'002' ,      -- rut - nvarchar(4000)
             N'Product2' , -- ProductCode - nvarchar(4000)
             2             -- Count - int
        );
    
    INSERT INTO dbo.UnionTest2 ( rut ,
                                 ProductGroupCode ,
                                 Count )
    VALUES ( N'001' ,           -- rut - nvarchar(4000)
             N'ProductGroup1' , -- ProductGroupCode - nvarchar(4000)
             1                  -- Count - int
        );
    
    INSERT INTO dbo.UnionTest2 ( rut ,
                                 ProductGroupCode ,
                                 Count )
    VALUES ( N'002' ,           -- rut - nvarchar(4000)
             N'ProductGroup1' , -- ProductGroupCode - nvarchar(4000)
             2                  -- Count - int
        );
    INSERT INTO dbo.UnionTest2 ( rut ,
                                 ProductGroupCode ,
                                 Count )
    VALUES ( N'002' ,           -- rut - nvarchar(4000)
             N'ProductGroup3' , -- ProductGroupCode - nvarchar(4000)
             3                  -- Count - int
        );

    查询两张表中的数据

    SELECT *
    FROM   dbo.UnionTest1;
    SELECT   *
    FROM     dbo.UnionTest2
    ORDER BY ProductGroupCode;

     进行union

    只能对union后的结果进行排序

    SELECT *
    FROM   dbo.UnionTest1
    UNION
    SELECT *
    FROM   dbo.UnionTest2
    ORDER BY ProductGroupCode;

  • 相关阅读:
    树形结构基础
    最长公共子序列
    四 过滤模式 map Only
    三 概要模式 2) MR倒排索引、性能分析、搜索干扰词。
    三 概要模式 1)数值概要 (单词计数记录计数最大值/最小值/计数平均值、中位数、标准差)
    一 梳理 从 HDFS 到 MR。
    个人学习源码的 HBase误区的总结 与 架构图
    15 hbase 学习(十五)缓存机制以及可以利用SSD作为存储的BucketCache
    13 hbase源码系列(十三)缓存机制MemStore与Block Cache
    HBase 系统架构
  • 原文地址:https://www.cnblogs.com/chucklu/p/8621841.html
Copyright © 2011-2022 走看看