您的位置:首页 > 数据库 > Oracle

oracle合并两个不一样的结果集

2017-08-11 16:36 295 查看

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


这样就在一个结果集里体现出来了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: