日期:2014-05-18 浏览次数:20599 次
SELECT A.* FROM A INNER JOIN ( SELECT AID,SUM(STR) AS TOTAL FROM B GROUP BY AID ) B ON A.ID=B.AID AND B.TOTAL>=A.VALUE
------解决方案--------------------
select a.* from tba a outer apply (select sum(str) str from tbb where aid = a.id) b where a.str <= b.str
------解决方案--------------------
declare @A table 
(id int ,value  float)
insert into @A
select 1,50 union all
select 2,30 union all
select 3,40 
declare @B table 
( id int ,aid int ,[str]  float )
insert into @B
select 1,1,10 union all
select 2,1,20 union all
select 3,2,30 union all
select 4,3,20 union all
select 5,3,20 
SELECT a.* FROM (
select AID,SUM([str]) AS vALUE 
from @B 
GROUP BY AID)B LEFT JOIN @A A on b.AID = A.id 
where b.vALUE >= a.value