oracle表交集并集,用sql求交集并集的问题

再看一下,是否满足你的要求。

交集:

SQL> with tmp1 as(

2  select 2 a from dual

3  union all

4  select 2 from dual

5  union all

6  select 3 from dual

7  union all

8  select 4 from dual

9  union all

10  select 4 from dual

11  union all

12  select 5 from dual

13  ),

14  tmp2 as(

15  select 2 a from dual

16  union all

17  select 2 from dual

18  union all

19  select 2 from dual

20  union all

21  select 3 from dual

22  union all

23  select 3 from dual

24  union all

25  select 4 from dual

26  union all

27  select 5 from dual

28  union all

29  select 5 from dual

30  )

31  select replace(wm_concat(i),',',chr(10)) y

32  from(

33     select case when a_num < b_num

34                 then (select wm_concat(a) from dual connect by rownum <= a_num)

35                 else (select wm_concat(a) from dual connect by rownum <= b_num)

36            end i

37       from(

38          select a_num,x.a,b_num

39       from(select a_num,a from(

40                              select count(*) a_num,a

41                                     from tmp1

42                                    group by a

43                                    order by a)

44            ) x,

45           (select b_num,a from(

46                             select count(*) b_num,a

47                                    from tmp2

48                                   group by a

49                                   order by a)

50            ) y

51      where x.a = y.a

52       )

53  )

54  /

Y

------------

2

2

3

4

5

SQL>

其中,把33行的”="即可得到你的并集。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值