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
这样就在一个结果集里体现出来了。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: oracle中sql语句中多个查询结果的交集、差集和并集
- 下一篇: oracle的sql查询结果拼接