求教一个高效率的sql

stven0 2013-04-24 11:03:23
用表B中的 net_amount 字段 更新表A中 net_amout 的数据.

两表的关联关系是B.quote_oid = A.oid

A与B表的关系是1:n

B表中取值的条件是 line_num 为最小的一条,并且status不等于40



参考数据如下:

A: oid net_amout
1 100

B: oid quote_oid line_num status net_amout
100 1 1 40 101
101 1 2 10 102
103 1 3 10 103



最终我们得到的结果应该为

A: OID NET_AMOUNT
1 102
...全文
193 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
stven0 2013-04-24
  • 打赏
  • 举报
回复
引用 3 楼 u010412956 的回复:
引用 2 楼 stven0 的回复:引用 1 楼 u010412956 的回复:merge into a using (select b.quote_oid, b.net_amout, row_number() over(partition by b.quote_oid order by line_num) r……
因为公司DBA 蛋疼啊
u010412956 2013-04-24
  • 打赏
  • 举报
回复
引用 2 楼 stven0 的回复:
引用 1 楼 u010412956 的回复:merge into a using (select b.quote_oid, b.net_amout, row_number() over(partition by b.quote_oid order by line_num) rn from b……
为什么要sql92的,,oracle特有的不行?
stven0 2013-04-24
  • 打赏
  • 举报
回复
引用 1 楼 u010412956 的回复:
merge into a using (select b.quote_oid, b.net_amout, row_number() over(partition by b.quote_oid order by line_num) rn from b where b.status <> 40) t ……
这不能过SQL92标准吧?
u010412956 2013-04-24
  • 打赏
  • 举报
回复
merge into a using (select b.quote_oid, b.net_amout, row_number() over(partition by b.quote_oid order by line_num) rn from b where b.status <> 40) t on (a.id = t.quote_oid and t.rn = 1) when matched then update set a. net_amount = t.net_amout;
stven0 2013-04-24
  • 打赏
  • 举报
回复
引用 5 楼 xinpingf 的回复:
quote_oid + line_num + status 是否唯一? SQL code?12345678910update a set net_amount = (select net_amount from b where b.quote_oid = a.oid and statu……
不是唯一的,你这关联出来数据多太多了
xinpingf 2013-04-24
  • 打赏
  • 举报
回复
这个是标准sql了吧
xinpingf 2013-04-24
  • 打赏
  • 举报
回复
quote_oid + line_num + status 是否唯一?
update a
   set net_amount =
        (select net_amount
           from b
          where b.quote_oid = a.oid
            and status <> 40
            and line_num = (select min(line_num)
                              from b c
                             where c.quoto_oid = a.oid
                               and status <> 40))

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧