update a set a.单价 = ( select b.金额/b.数量 from b,(select 编码,max(日期) 日期 from b group by 编码) b_1 where b.编码 = b_1.编码 and b.日期 = b_1.日期 ) where exists (select 1 from b where 编码 = a.编码)
update a set 单价 =( select t.单价1 from ( select r.金额/r.数量 单价1, 编码 from (select b.* ,rank() over (partition by 编码 order by 日期 desc) rownum2 from b ) r where rownum2 < 2 ) t where a.编码 = t.编码)
update a set 价格=(select p from (select 编码,(金额/数量) p from b where (编码,日期) in (select 编码,max(日期) from b group by 编码)) bt where a.编码=bt.编码);
表a(编码、单价),和表b(编码、日期、数量、金额),现要修改a表的字段单价 公式:a(单价)=b(金额)/b(数量),条件:a(编码)=b(编码)要从b里最大的日期记录选。想得到个快速的查询方法,向大家请教了。 1。可以先倍分表b,在b中将 a(编码)=b(编码)且 b 的日期不是最大的删除掉。 这样就是一一对应关系。 delete from b where 日期<(select max(日期) from b where 编码=b.编码)2.直接update 就可以了。 update a set 价格=(select b.金额 / b.数量 from a,b where a.编码 = b.编码)
update a set 单价 =( select t.单价1 from ( select r.金额/r.数量 单价1, 编码 from (select b.* ,rank() over (partition by 编码 order by 日期 desc) rownum2 from b ) r where rownum2 < 2 ) t where a.编码 = t.编码) 有错误嘛??把错误贴出来看看
update a set a.单价 = ( select bb.金额 / bb.数量 from (select * from b b1 where( b1.编码,b1.日期 ) in (select b.编码,max(b.日期) from b group by b.编码)) bb where a.编码 = bb.编码 )
我修改了一下男人海洋的代码,你看看我没有测试 update a set a.单价 = (select b.金额/b.数量 from b,(select distinct 编码,max(日期) 日期 from b group by 编码) b_1 where b.编码 = b_1.编码) where exists (select 1 from b where b.编码 = a.编码)
update a set a.单价 = ( select b.金额 / b.数量 from b,(select max(c.日期),max(编码) from c group by c.编码) t where b.编码 = t.编码 and b.日期 = t.日期 and b.编码 = t.编码 )
用这个方法可以实现你的要求,加分吧:) update a set a.单价 = (select 金额/数量 from b where a.编码 =b.编码 And b.日期=(select max(日期) from b) ) ;
看看下面的运行记录,是不是你要的效果?给分哦 注意,时间要精确到秒,只精确到日期的话可能会出错。实际上,精确到秒也可能会出错,但几率就非常非常小了SQL> select * from csdn_price; PNO PRICE ---------- ---------- 1 0 2 0 3 0 4 0SQL> select * from csdn_info; NO PDATE CNT BALANCE ---------- -------------------- ---------- ---------- 1 2005-5-14 22:18:50 2 10 1 2005-5-15 22:19:06 3 21 1 2005-5-16 22:19:26 5 80 2 2005-5-14 22:19:38 1 20 2 2005-5-15 22:19:51 9 100 3 2005-5-14 22:20:02 10 3006 rows selectedSQL> SQL> update csdn_price a set price=(select balance/cnt pri from 2 (select * from csdn_info where pdate in 3 (select max(pdate) from csdn_info group by no)) b where a.pno=b.no) 4 /4 rows updatedSQL> select * from csdn_price; PNO PRICE ---------- ---------- 1 16 2 11.1111111 3 30 4
楼上的,你的update把csdn_price中的pno=4的price的值给弄没了,这个不行吧。
你看看我测试过 update a set a.单价 = b_1.单价 from a,(select distinct 编码,max(日期) 日期,金额/数量 单价 from b group by 编码) b_1 where a.编码 = b_1.编码
谁知道space621的代码中,能不能利用decode把pno = 4的值保留下来。
update A表 a set a.单价 = ( select c.金额/c.数量 from B表 c where c.date = ( select max(b.日期) from B表 b where a.编码 = b.编码 group by b.日期 ) and rownum < 2 )
update A表 a set a.单价 = ( select c.金额/c.数量 from B表 c where c.日期 = ( select max(b.日期) from B表 b where a.编码 = b.编码 group by b.日期 ) and rownum < 2 )
update A表 a set a.单价 = ( select c.金额/c.数量 from B表 c where c.日期 = ( select max(b.日期) from B表 b where c.编码 = b.编码 group by b.编码 ) and c.编码 = a.编码 and rownum < 2 ) where a.编码 in ( select distinct 编码 from B表)不好意思,前面是凭想象写的,这个是经过测试的,没问题。不过写的让人看着就闹心。
试试这样 update a set dj = (select dj from (select distinct bm,FIRST_VALUE(je/shl) OVER (PARTITION BY bm ORDER BY rq DESC) dj from b) c where a.bm = c.bm ) where exists (select 1 from b where b.bm = a.bm)
update a set 单价 =( select t.单价1 from ( select r.金额/r.数量 单价1, 编码 from (select b.* ,row_number() over (partition by 编码 order by 日期 desc) rownum2 from b // row_number是一个分析函数,你学习一下 ) r where rownum2 < 2 //在 b表里面 按编码分区 ,日期降序排序,找出每个编码的 第一条纪录,并球出单价 ) t where a.编码 = t.编码)//按条件:a(编码)=b(编码)更新记录 !!我试过的 ,早就可以了,你自己不去试别人写的那也没办法了
典型的数据不一致,不是数据库设计人员水平的问题就是程序员水平的问题改写的时候加上一个rownum=1限制就可以了update a set a.单价 = (select b.金额/b.数量 from b,(select 编码,max(日期) 日期 from b group by 编码) b_1 where b.编码 = b_1.编码 and b.日期 = b_1.日期 and rownum=1 ) where exists (select 1 from b where 编码 = a.编码)
set a.单价 = ( select b.金额/b.数量
from b,(select 编码,max(日期) 日期 from b group by 编码) b_1
where b.编码 = b_1.编码
and b.日期 = b_1.日期
)
where exists (select 1 from b where 编码 = a.编码)
select t.单价1 from (
select r.金额/r.数量 单价1, 编码 from
(select b.* ,rank() over (partition by 编码 order by 日期 desc) rownum2 from b ) r
where rownum2 < 2
) t
where a.编码 = t.编码)
公式:a(单价)=b(金额)/b(数量),条件:a(编码)=b(编码)要从b里最大的日期记录选。想得到个快速的查询方法,向大家请教了。
1。可以先倍分表b,在b中将 a(编码)=b(编码)且 b 的日期不是最大的删除掉。
这样就是一一对应关系。
delete from b where 日期<(select max(日期) from b where 编码=b.编码)2.直接update 就可以了。
update a set 价格=(select b.金额 / b.数量 from a,b where a.编码 = b.编码)
select t.单价1 from (
select r.金额/r.数量 单价1, 编码 from
(select b.* ,rank() over (partition by 编码 order by 日期 desc) rownum2 from b ) r
where rownum2 < 2
) t
where a.编码 = t.编码)
有错误嘛??把错误贴出来看看
update a
set a.单价 = (select b.金额/b.数量
from b,(select distinct 编码,max(日期) 日期 from b group by 编码) b_1
where b.编码 = b_1.编码)
where exists (select 1 from b where b.编码 = a.编码)
a表结构 bm dj b表结构 bm rq shl je
1 1 1 '23-mar-05' 10 100
2 2 2 '24-mar-05' 20 4000
3 3 2 '25-mar-05' 20 8000
4 4
select b.金额 / b.数量
from b,(select max(c.日期),max(编码) from c group by c.编码) t
where b.编码 = t.编码 and b.日期 = t.日期 and b.编码 = t.编码 )
update a set a.单价 = (select 金额/数量 from b
where a.编码 =b.编码 And b.日期=(select max(日期) from b) ) ;
注意,时间要精确到秒,只精确到日期的话可能会出错。实际上,精确到秒也可能会出错,但几率就非常非常小了SQL> select * from csdn_price; PNO PRICE
---------- ----------
1 0
2 0
3 0
4 0SQL> select * from csdn_info; NO PDATE CNT BALANCE
---------- -------------------- ---------- ----------
1 2005-5-14 22:18:50 2 10
1 2005-5-15 22:19:06 3 21
1 2005-5-16 22:19:26 5 80
2 2005-5-14 22:19:38 1 20
2 2005-5-15 22:19:51 9 100
3 2005-5-14 22:20:02 10 3006 rows selectedSQL>
SQL> update csdn_price a set price=(select balance/cnt pri from
2 (select * from csdn_info where pdate in
3 (select max(pdate) from csdn_info group by no)) b where a.pno=b.no)
4 /4 rows updatedSQL> select * from csdn_price; PNO PRICE
---------- ----------
1 16
2 11.1111111
3 30
4
update a
set a.单价 = b_1.单价
from a,(select distinct 编码,max(日期) 日期,金额/数量 单价 from b group by 编码) b_1
where a.编码 = b_1.编码
set a.单价 = ( select c.金额/c.数量 from B表 c where c.date =
( select max(b.日期)
from B表 b
where a.编码 = b.编码
group by b.日期
)
and rownum < 2
)
set a.单价 = ( select c.金额/c.数量 from B表 c where c.日期 =
( select max(b.日期)
from B表 b
where a.编码 = b.编码
group by b.日期
)
and rownum < 2
)
set a.单价 = ( select c.金额/c.数量 from B表 c
where c.日期 =
( select max(b.日期)
from B表 b
where c.编码 = b.编码
group by b.编码
)
and c.编码 = a.编码
and rownum < 2
)
where a.编码 in ( select distinct 编码 from B表)不好意思,前面是凭想象写的,这个是经过测试的,没问题。不过写的让人看着就闹心。
update a set dj =
(select dj from
(select distinct bm,FIRST_VALUE(je/shl) OVER (PARTITION BY bm ORDER BY rq DESC) dj
from b) c
where a.bm = c.bm
)
where exists (select 1 from b where b.bm = a.bm)
select t.单价1 from (
select r.金额/r.数量 单价1, 编码 from
(select b.* ,row_number() over (partition by 编码 order by 日期 desc) rownum2 from b
// row_number是一个分析函数,你学习一下
) r
where rownum2 < 2
//在 b表里面 按编码分区 ,日期降序排序,找出每个编码的 第一条纪录,并球出单价
) t
where a.编码 = t.编码)//按条件:a(编码)=b(编码)更新记录 !!我试过的 ,早就可以了,你自己不去试别人写的那也没办法了
set a.单价 = (select b.金额/b.数量
from b,(select 编码,max(日期) 日期 from b group by 编码) b_1
where b.编码 = b_1.编码
and b.日期 = b_1.日期
and rownum=1
)
where exists (select 1 from b where 编码 = a.编码)