表tab1 如下
ddbh sl je bhbj(有变化标记)
-------------------
A 1 1 1
A 2 2
B 1 1 1
B 3 3 1原来表tab2如下
ddbh sl je
------------
A 2 2
最后要tab2表的数据
ddbh sl je
------------
A 3 3
B 4 4
我要做的工作是:
1、将tab2中没有的ddbh,在tab1中有的汇总数插入。
2、如果tab1中某个ddbh,如A的数据增加(如果在tab1中加标志记录一下有改变的数据),tab2重新计算A的数量,不能用sum,只能用2+1=3,因为tab2是个基数。
最后要出tab2的结果是正确的汇总数据。
3、不要整体汇总,只是在tab1中有标记变化的数据才汇总tab2中。
请帮忙,好久不用ORCLEL了
我用的语句感觉很不好:
insert into tab2(je,sl)
select a.ddbh,sum(a.je) zs ,sum(a.sl) ms from tab1 a where a.ddbh not in (select ddbh from tab2) and a.bhbj=1 group by a.ddbh update tab2 set sl=sl+(select sum(sl) from tab1 where tabl.ddbh=tab2.ddbh and tb1.bhbj=1), je=je+(select sum(je) from tab1 where tabl.ddbh=tab2.ddbh and tb1.bhbj=1),
ddbh sl je bhbj(有变化标记)
-------------------
A 1 1 1
A 2 2
B 1 1 1
B 3 3 1原来表tab2如下
ddbh sl je
------------
A 2 2
最后要tab2表的数据
ddbh sl je
------------
A 3 3
B 4 4
我要做的工作是:
1、将tab2中没有的ddbh,在tab1中有的汇总数插入。
2、如果tab1中某个ddbh,如A的数据增加(如果在tab1中加标志记录一下有改变的数据),tab2重新计算A的数量,不能用sum,只能用2+1=3,因为tab2是个基数。
最后要出tab2的结果是正确的汇总数据。
3、不要整体汇总,只是在tab1中有标记变化的数据才汇总tab2中。
请帮忙,好久不用ORCLEL了
我用的语句感觉很不好:
insert into tab2(je,sl)
select a.ddbh,sum(a.je) zs ,sum(a.sl) ms from tab1 a where a.ddbh not in (select ddbh from tab2) and a.bhbj=1 group by a.ddbh update tab2 set sl=sl+(select sum(sl) from tab1 where tabl.ddbh=tab2.ddbh and tb1.bhbj=1), je=je+(select sum(je) from tab1 where tabl.ddbh=tab2.ddbh and tb1.bhbj=1),
解决方案 »
- oracle 取随机数(涉及概率)问题
- where 语句的一个判断怎么根据传入的条件生效
- 求oracle双机热备方案
- ORA-00604: 递归 SQL 级别 2 出现错误 ora-00942 表或者视图不存在
- 如何在子cursor的where 条件中处理null的情况
- oracle tab 表查询错误
- 关于闪回表的问题,报ORA-08180: no snapshot found based on specified time错误
- Sql查询问题,请指教!!!
- 我想把一个oracle的查询转成mysql的,不知道这里有没有高手帮我实现一下。搞定了,把这边和那边(myssql论坛)的共200分都给你
- 请众位大侠帮忙看一下这个错误!!!
- 求一SQL语句
- 手动安装数据库的怪问题,一定要高手
你每入出一笔,都要计算一下,只能在原来数据基础上加减,而不能用SUM.
那你用not exists 吧!
1、将tab2中没有的ddbh,在tab1中有的汇总数插入。
insert into tab2 (select t1.ddbh, sum(t1.sl), sum(t1.je) from tab1 t1 where not exist (select 1 from tab2 t2 where t2.ddbh=t1.ddbh))2、如果tab1中某个ddbh,如A的数据增加(如果在tab1中加标志记录一下有改变的数据),tab2重新计算A的数量,不能用sum,只能用2+1=3,因为tab2是个基数。
最后要出tab2的结果是正确的汇总数据。
update tab2 t2 set(sl,je) (select t2.sl+temp.sl, t2.je+temp.sl from (select sum(sl) sl, sum(je) je ddbh from tab1 t1 where t1.bhbj=1 and t2.bhbj=t1.bhbj) temp)时间不够,还没有测试过,你看这个逻辑对不对