问题是:
有a, b 两个表, 分别有id1 和 id2 关键字段互相关联,关联条件是
b 表中要找出与a 表相同id1, 并且 id2 <= a.id2 中最大的一条相关联. 另:此语法在 sql 中正常, 在 oracle 中报错:"列不能外部连接到子查询". 求在oracle 中的写法!!! 请高手出招, mail: [email protected] SELECT a.*, b.* FROM a LEFT JOIN b ON a.id1=b.id1 AND b.id2=(select max(id2) from b where id1=a.id1 and id2<=a.id2) WHERE (some condition)
有a, b 两个表, 分别有id1 和 id2 关键字段互相关联,关联条件是
b 表中要找出与a 表相同id1, 并且 id2 <= a.id2 中最大的一条相关联. 另:此语法在 sql 中正常, 在 oracle 中报错:"列不能外部连接到子查询". 求在oracle 中的写法!!! 请高手出招, mail: [email protected] SELECT a.*, b.* FROM a LEFT JOIN b ON a.id1=b.id1 AND b.id2=(select max(id2) from b where id1=a.id1 and id2<=a.id2) WHERE (some condition)
解决方案 »
- 请教一个ORACLE中遇到多次都无法解决的问题
- 怎么修改已经建好的表中某一列的TITLE内容
- 安装oracle出现问题了
- !!!求助Oracle高手,Oracle服务自动停止问题!
- PL/SQL登录问题?
- Oracle sqldeveloper 中的法语(如Peña)导出到csv的时候显示为“Pe帽a”, 如何让它直接显示成Peña ?
- oracle中用户的表的删除与新增
- 求一条SQL语句,一棵树,只要求把叶子找出来。
- 登陆Management Server mserver问题:急!!!(给分)
- 关于字符集的问题
- oracle游标如何改写成sqlserver游标写法?
- string 插入数据库遇到的神奇问题?
and (some condition)
and (some condition)
and (some condition)union
select * from a,b where a.id1=b.id1(+) where b.id1 is null
from(
select a.*,b.*,
row_number() over (partition by a.id1 order by b.id2 desc) rn
from a,b
where a.id1=b.id1(+)
and b.id2<=a.id2) aa
where aa.rn=1
表比较多,每张表中的记录也比较多的话,建议不要用sql语句实现;
用存储过程吧
SELECT a.*, b.*,(or other) FROM a LEFT JOIN b ON a.id1=b.id1 AND b.id2=(select max(id2) from b where id1=a.id1 and id2 <=a.id2) LEFT JOIN c ON a.id1=c.id1 AND c.id2=(select max(id2) from c where id1=a.id1 and id2 <=a.id2) LEFT JOIN d ON a.id1=d.id1 AND d.id2=(select max(id2) from d where id1=a.id1 and id2 <=a.id2)(...) WHERE (条件包含 a,b,c,d 中的字段)以上是我在 sql 中成功运行了的, 感觉可阅读性较好, 求在 oracle 中的写法, 非赏感谢!!!