22,209
社区成员
发帖
与我相关
我的任务
分享
if object_id('tb','u') is not null
drop table tb
go
create table tb (机构号 int,客户号 int,帐号 int,客户名称 varchar(5),月日均 numeric(6,2))
insert into tb
select 1701,2006,23891,'公司1',5000.00 union all
select 1701,2006,23932,'公司1',3000.00 union all
select 1701,2007,28761,'公司2',2300.00 union all
select 1702,2007,28762,'公司2',1198.00
go
select a.*,s 日月均和 from tb a
inner join
(select 客户号,sum(月日均) s from tb group by 客户号) b
on a.客户号=b.客户号
go
/*
1701 2006 23891 公司1 5000.00 8000.00
1701 2006 23932 公司1 3000.00 8000.00
1701 2007 28761 公司2 2300.00 3498.00
1702 2007 28762 公司2 1198.00 3498.00
*/
select xxx,xxx
from
(
select *,月日均和=(select sum(月日均) from V_对公存款 where 客户号=a.客户号)
from V_对公存款 a
) a
outer join xxx
on xx=xx
where xx=xx
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (机构号 int,客户号 int,帐号 int,客户名称 varchar(5),月日均 numeric(6,2))
insert into #T
select 1701,2006,23891,'公司1',5000.00 union all
select 1701,2006,23932,'公司1',3000.00 union all
select 1701,2007,28761,'公司2',2300.00 union all
select 1702,2007,28762,'公司2',1198.00
select *,月日均和=(select sum(月日均) from #T where 客户号=t.客户号)
from #T t
/*
机构号 客户号 帐号 客户名称 月日均 月日均和
----------- ----------- ----------- ----- -------- ----------------------------------------
1701 2006 23891 公司1 5000.00 8000.00
1701 2006 23932 公司1 3000.00 8000.00
1701 2007 28761 公司2 2300.00 3498.00
1702 2007 28762 公司2 1198.00 3498.00
(所影响的行数为 4 行)
*/
select *,月日均和=(select sum(月日均) from tb where 客户号=t.客户号)
from tb t