union和union all用法

工作中,遇到同事之前写的oracle语句中有一个union all,并且很多地方都用到了。便在网上查了一下用法,以下是自己的理解。

union  (联合)将两个或者多个结果集合并。

在使用时,两个结果集要有相同的列,并且字段类型需要一致。

select id,name from tableA
union all
select name from tableB

消息 205,级别 16,状态 1,第 1 行
使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式。

union 和union all 的区别;

union会去除结果集中重复的部分,相当于进行一个distinct(去重),并且union 会自带排序功能;

union all 会不管是否重复,都会将结果合并在一起输出,没有排序功能,只是结果集的堆叠输出。

tableA

id  name  score

1   a    80

2     b    79

3     c      68

tableB

id  name  score

1     d    48

2   e    23

3   c    86

使用union

1、                    结果如下:name

select name from tableA                a

union                         b

select name from tableB                c

                             d

                           e

我们运行

select id,name from tableA

union

select id,name from tableB

结果如下:

id name

1 a
1 d
2 b
2 e
3 c

两个表中都有 3 c ,使用union时只输出一次。

并且我们会发现,union会按照第一列进行默认排序。

使用union all

1、

select name from tableA
union all
select name from tableB

结果:

name

a
b
c
d
e
c

2、

select id,name from tableA
union all
select id,name from tableB

结果如下:

id name

1 a
2 b
3 c
1 d
2 e
3 c

从结果看到,两个union all 结果差别只是在于是否输出id 其输出顺序,为 tableA所有记录紧接tableB所有记录,因此说union all非排序输出。

上边的用法应该在很多地方都可以查到吧。

下面说一下我遇到的问题。

在业务中需要查询两列,两个不同的列从两个表中获取。

select
t.d day_id,
sum(t.OWN_COST) own_cost,
sum(t.cishu) cishu
from 
(
    select 
    to_char(f.riqi,'yyyy-mm-dd')d ,
    sum(nvl(f.feiyong1, 0))
    + sum(nvl(f.feiyong2, 0)) OWN_COST,--金额
    0  cishu
    from tablea t ,tableb f
    where 
    t.liushuihao=f.liushuihao 

    group by to_char(f.ji_fei_rq ,'yyyy-mm-dd')
 
    union all
     
    SELECT 
    to_char(jiaoyiriqi  ,'yyyy-mm-dd') d,
    0 OWN_COST,
    COUNT(case
    when JIAO_YI_LX = 1 then --【交易类型,1正交易,2反交易】
    1
    end) - COUNT(case
    when JIAO_YI_LX = 2 then --【交易类型,1正交易,2反交易】
    1
    end) cishu  
    FROM tablea 
     
    group by to_char(jiaoyiriqi,'yyyy-mm-dd')
 )t
 group by t.d

以上代码看到两个子查询中都有次数和金额。当子查询计算金额的时候,设置默认的此时为0    (0 次数);当计算次数的时候,设置金额为0 (0 own_cost)。

这样写的好处:

 1、当次数有问题时,我们只需要查看计算次数的子查询部分,同理,金额错误时我们只需要查看相关代码就可以。

   2、在子查询中,设置不进行计算的值为0,对于运算结果并不会产生影响。

以上是本人对union的浅显理解,欢迎各位大神指导。

原文地址:https://www.cnblogs.com/com-xiaolanchong/p/6121048.html