22,209
社区成员
发帖
与我相关
我的任务
分享
create table t1(编号 int,金额 int)
insert into t1 select 101,50
insert into t1 select 102,40
insert into t1 select 103,10
create table t2(编号 int,费用 int,月份 varchar(10),标志 bit)
insert into t2 select 101,5,'201001',0
insert into t2 select 101,6,'201002',0
insert into t2 select 102,5,'201001',0
insert into t2 select 103,5,'201007',0
insert into t2 select 103,8,'201008',0
update t2
set 标志=1
from t1,
(select *,(select sum(费用) from t2 as tb where tb.编号=t2.编号 and tb.月份<=t2.月份)总费用 from t2) t3
where t2.编号=t3.编号 and t2.月份=t3.月份 and t1.编号=t3.编号 and t1.金额>t3.总费用
update t1 set 金额=金额-
(select top 1 总费用 from
(select 编号,(select sum(费用) from t2 as tb where tb.编号=t2.编号 and tb.月份<=t2.月份)总费用 from t2 ) t3
where t1.编号=t3.编号 and t3.总费用<t1.金额 order by 总费用 desc)
select * from t1
select * from t2
drop table t1,t2
/*
编号 金额
----------- -----------
101 39
102 35
103 5
(所影响的行数为 3 行)
编号 费用 月份 标志
----------- ----------- ---------- ----
101 5 201001 1
101 6 201002 1
102 5 201001 1
103 5 201007 1
103 8 201008 0
(所影响的行数为 5 行)
*/
create table t1(编号 int,金额 int)
insert into t1 select 101,50
insert into t1 select 102,40
insert into t1 select 103,10
create table t2(编号 int,费用 int,月份 varchar(10))
insert into t2 select 101,5,'201001'
insert into t2 select 101,6,'201002'
insert into t2 select 102,5,'201001'
insert into t2 select 103,5,'201007'
insert into t2 select 103,8,'201008'
go
;with c1 as(
select row_number()over(partition by 编号 order by 月份)rn,编号,费用,月份 from t2
),c2 as(
select b.rn,a.编号,a.金额-b.费用 金额 from t1 a inner join c1 b on a.编号=b.编号 where b.rn=1
union all
select b.rn,a.编号,a.金额-b.费用 金额 from c2 a inner join c1 b on a.编号=b.编号 and a.rn=b.rn-1 where a.金额-b.费用>=0
)select a.编号,a.费用,a.月份,isnull(sign(b.金额),0) as 已扣 from c1 a left join c2 b on a.rn=b.rn and a.编号=b.编号
/*
编号 费用 月份 已扣
----------- ----------- ---------- -----------
101 5 201001 1
101 6 201002 1
102 5 201001 1
103 5 201007 1
103 8 201008 0
(5 行受影响)
*/
go
drop table t1,t2
create table t1(编号 int,金额 int)
insert into t1 select 101,50
insert into t1 select 102,40
insert into t1 select 103,10
create table t2(编号 int,费用 int,月份 varchar(10))
insert into t2 select 101,5,'201001'
insert into t2 select 101,6,'201002'
insert into t2 select 102,5,'201001'
insert into t2 select 103,5,'201007'
insert into t2 select 103,8,'201008'
go
;with c1 as(
select row_number()over(partition by 编号 order by 月份)rn,编号,费用 from t2
),c2 as(
select b.rn,a.编号,a.金额-b.费用 金额 from t1 a inner join c1 b on a.编号=b.编号 where b.rn=1
union all
select b.rn,a.编号,a.金额-b.费用 金额 from c2 a inner join c1 b on a.编号=b.编号 and a.rn=b.rn-1 where a.金额-b.费用>=0
)select 编号,金额 from c2 a where not exists(select 1 from c2 where 编号=a.编号 and 金额<a.金额)
order by 编号
/*
编号 金额
----------- -----------
101 39
102 35
103 5
(3 行受影响)
*/
go
drop table t1,t2
update 表1 set 金额=金额-费用 from 表1 left outer join
(
select 编号 ,sum(费用) as 费用 from 表2 group by 编号
) t2 on 表1.编号=t2.编号
update a
set a.金额=金额-b.费用
from 表1 a join (select 编号,sum(费用) as 费用 from 表2 group by 编号) b on a.编号=b.编号
create table t1(编号 int,金额 int)
insert into t1 select 101,50
insert into t1 select 102,40
insert into t1 select 103,60
create table t2(编号 int,费用 int,月份 varchar(10))
insert into t2 select 101,5,'201001'
insert into t2 select 101,6,'201002'
insert into t2 select 102,5,'201001'
insert into t2 select 103,5,'201007'
insert into t2 select 103,8,'201008'
go
update a set 金额=金额-b.fy from t1 a inner join(
select 编号,sum(费用)fy from t2 group by 编号
)b on a.编号=b.编号
select * from t1
/*
编号 金额
----------- -----------
101 39
102 35
103 47
(3 行受影响)
*/
go
drop table t1,t2
update 表1 set 金额=金额-tb.费用
from (select sum(费用) as 费用 from 表2 group by 编号) tb
where 表1.编号=tb.编号
update 表1 set 金额=金额-a.费用
from (select sum(费用) as 费用 from 表2 group by 编号) tb
where 表1.编号=tb.编号