Select a.name ,mamoubt=(Select sum(amount) From 表 where id<10 And name=a.name),
       namoubt=(Select sum(amount) From 表 where id>=50 And name=a.name)
from 表 a Group By a.name

解决方案 »

  1.   

    Select a.name ,mamoubt=(Select sum(amount) From 表 where id<10 And name=a.name),
           namoubt=(Select sum(amount) From 表 where id>=50 And name=a.name)
    from 表 a Group By a.name
      

  2.   

    Select name, 
    mamoubt=(select sum(convert(float,amount))  from test2 where convert(int,id)<10 and name=a.name ),
    mamoubt=(select sum(convert(float,amount))  from test2 where convert(int,id)>=50 and name =a.name )
    from test2 a
    Group By name
      

  3.   

    Select name, 
    mamoubt=(select sum(convert(float,amount))  from test2 where convert(int,id)<10 and name=a.name ),
    mamoubt=(select sum(convert(float,amount))  from test2 where convert(int,id)>=50 and name =a.name )
    from test2 a
    Group By name
      

  4.   

    对于相同的name,id<10,与id>=50 的数据相加分开,id<10的相加放到mamount字段,id>=50的相加放到namount字段select name, amount=select sum(amount) from talbe a where a.id<10 and name=a.name
    mamoubt=select sum(amount) from table) a where b.id>50 and name=b.name
    from table
    group name
      

  5.   

    select name ,
           sum(case when id<10 then amount else 0 end ) as mamoubt,
           sum(case when id>50 then amount else 0 end ) as namoubt
      from table 
      group by name
      

  6.   

    select name ,
           sum(case when id<10 then amount else 0 end ) as mamoubt,
           sum(case when id>50 then amount else 0 end ) as namoubt
      from table 
      group by name这个是最好的呀!