--try itselect
t2.id,
t2.departmentname,
(select sum(tt2.countperson) from table2 tt2
where tt2.id=t2.id or tt2.id in (select t1.id from table1 t1 where t1.topid=t2.id )
) as total
from table2 t2
t2.id,
t2.departmentname,
(select sum(tt2.countperson) from table2 tt2
where tt2.id=t2.id or tt2.id in (select t1.id from table1 t1 where t1.topid=t2.id )
) as total
from table2 t2
解决方案 »
- 关于数据库被注入C.JS那个病毒
- 写一个连续记录大于4的sql语句 跪求
- insert into tempcode ( iid ) values ( select iid from km )
- PHP 执行SQL server 存储过程的问题请教
- 如何用脚本获得sql server作业的执行情况,谢谢!
- 行合并,急,急,急~~~~~~
- 问个关于SQL语句的问题, 很简单的, 属于抢分类型的, 来看看吧
- 对于大型数据库的优化都有那些方法
- 接手就可赚钱,出售一套foxpro写的适用于中、大型医院的管理系统
- 紧急求教:如何将*.RPT导入SQL数据库《在线等》
- 充装系统后,恢复以前的数据库备分,提示Unicode不一致,导致不成功
- 急救,有沒有連接n行數據的函數?字符型。就像統計數字的SUM函數一樣?
insert #table1 values(1, 2)
insert #table1 values(2 , 0)
insert #table1 values(3 , 2)
insert #table1 values(4 , 3)
insert #table1 values(5 , 3)
create table #table2(id int,countperson int,departmentname varchar(100))
insert #table2 values(1, 22 , '****')
insert #table2 values(2 , 11 , '**')
insert #table2 values(3 , 55 , '***')
insert #table2 values(4 , 545, '**')
insert #table2 values(5 , 44, '**')select *,null 人数 into #result from #table2declare @tmp1 table (Id int,pid int)while exists(select 1 from #result where 人数 is null)
begin insert @tmp1 select * from #table1 where ID=(select min(id) from #result where 人数 is null) while exists(select 1 from #table1 where topID in (select id from @tmp1) and id not in (select id from @tmp1))
insert @tmp1 select * from #table1 where TOPID in (select id from @tmp1) and id not in (select id from @tmp1) update #result set 人数=(select sum(countperson) from #table2 where id in (select id from @tmp1)) where id=(select min(id) from #result where 人数 is null)
delete @tmp1
end
select * from #result
go
drop table #table1,#table2,#result--注意 #table1,#table2 分别为你的表,#result是结果临时表 (SQLServer MVP 大力)