if object_id('T1') IS NOT NULL drop table T1create table T1(ypid int, cpid int ,shuliang int) insert into T1 select 1122,1088,1 union all select 1001,1088,5 union all select 152, 1088,8 if object_id('T2') IS NOT NULL drop table T2create table T2(ypid int, cpid int ,flag varchar(8)) insert into T2 select 1122,1088,'是' union all select 1001,1088,'是' union all select 152, 1088,'否'select sum(T1.shuliang) as '总数' from T2 inner join T1 on T1.ypid = T2.ypid AND T1.cpid = T2.cpid where flag = '是' /* 总数 ----------- 6(1 行受影响) */
select sum(shuliang)合计 from T2 join t2 on t1.cpid=t2.cpid where t2.flag='是'
drop table T1create table T1(ypid int, cpid int ,shuliang int)
insert into T1
select 1122,1088,1 union all
select 1001,1088,5 union all
select 152, 1088,8
if object_id('T2') IS NOT NULL
drop table T2create table T2(ypid int, cpid int ,flag varchar(8))
insert into T2
select 1122,1088,'是' union all
select 1001,1088,'是' union all
select 152, 1088,'否'select sum(T1.shuliang) as '总数' from T2 inner join T1 on T1.ypid = T2.ypid AND T1.cpid = T2.cpid
where flag = '是'
/*
总数
-----------
6(1 行受影响)
*/