有个表表结构数据
code  lin1  lin2 lin3  month
1001   11    22   33    1
1001   23    56    48   2
.
.
1002  255    626   5656  1
1002  626    23    232   2
...
1004
...
如何用实现
code       lin1              lin2        lin3      
1001    (month 为1的数据)   sum(lin2)   sum(lin3) 

解决方案 »

  1.   

    select m.code , m.lin1 , n.lin2 , n.lin3 from tb m,
    (select code,sum(lin2) lin2,sum(lin3) lin3 from tb group by code) n
    where m.month = 1 and m.code = n.code
      

  2.   

    select m.code , m.lin1 , n.lin2 , n.lin3 from 
    (select code,sum(lin2) lin2,sum(lin3) lin3 from tb group by code) n , tb m
    where m.month = 1 and m.code = n.code
      

  3.   


    if object_id('tb') is not null
       drop table tb
    go
    create table tb(code varchar(4),lin1 int,lin2 int,lin3 int,month int)
    go
    insert into tb
    select '1001',11,22,33,1 union all
    select '1001',23,56,48,2
    go
    select * from tbselect a.code,a.s2,a.s3 from tb inner join (
    select code,sum(lin2) s2,sum(lin3) s3 from tb group by code
    ) a on tb.code=a.code where tb.month=1
      

  4.   


    if object_id('tb') is not null
       drop table tb
    go
    create table tb(code varchar(4),lin1 int,lin2 int,lin3 int,month int)
    go
    insert into tb
    select '1001',11,22,33,1 union all
    select '1001',23,56,48,2
    go
    select * from tbselect a.code,tb.lin1,a.s2 as lin2,a.s3 as lin3 from tb inner join (
    select code,sum(lin2) s2,sum(lin3) s3 from tb group by code
    ) a on tb.code=a.code where tb.month=1