表:T1ypid   cpid   shuliang
1122   1088   1
1001   1088   5
 152   1088   8
表:T2ypid   cpid  flag
1122   1088  是
1011   1088  是
 152   1088  否
表T1.cpid=T2.cpid如何选出flag=是,cpid=1088的shuliang合计?

解决方案 »

  1.   

    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 行受影响)
    */
      

  2.   

    select sum(shuliang)合计 from T2 join t2 on t1.cpid=t2.cpid where t2.flag='是'