oracle查询两个结果集合并

1:通过union 和 union all 合并,但是前提了查出来的结果集要一致
2:如果两个结果集不一致,就要用到left join on

比如:
有a表,我想要求7月和8月的前三天的价格都是多少
select * from a;

月份 (month)日期(day)价格(price)
071$1600
072$12
073$1
07……
081$1500
082$11
083$2
08……

用一个sql完成就是这样:

              select a.price 当前月份价格,
              b.price 上月份价格,
              a.day 账期
              from (
                     select 
                     t.price,
                     t.day from a t
                      where  t.month = to_number('201708')
                       and t.day <= '03'
            ) a left join (
                     select 
                        t.price 上月份价格,
                        t.day 
                      from a t
                      where  t.month = to_number('201708')-1
                       and t.day <= '03'
            ) b on a.day=b.day
            order by a.day

这样就在一个结果集里体现出来了。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐