oracle查询两个结果集合并
1:通过union 和 union all 合并,但是前提了查出来的结果集要一致
2:如果两个结果集不一致,就要用到left join on
比如:
有a表,我想要求7月和8月的前三天的价格都是多少
select * from a;
| 月份 (month) | 日期(day) | 价格(price) |
|---|---|---|
| 07 | 1 | $1600 |
| 07 | 2 | $12 |
| 07 | 3 | $1 |
| 07 | … | … |
| 08 | 1 | $1500 |
| 08 | 2 | $11 |
| 08 | 3 | $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
这样就在一个结果集里体现出来了。