举个例子:
  NO    NAME    NUM
  1     AA      10
  2     BB      20
  1     CC      30
得到的正确结果为
  NO    NAME    sum(NUM)
  1     AA      40
  2     BB      20
 或者
  NO    NAME    sum(NUM)
  1     CC      40
  2     BB      20

解决方案 »

  1.   

    一个SQL语句如何能返回两种结果?确定要哪一组!
      

  2.   

    declare @no int,@name char(10)
    create table #temp(no int ,name char(10)) --创建一临时表
    declare temptable cursor for  --用游标向临时表写数据
      select distinct no,name      from yourtable
       open temptable    fetch from temptable into @no,@name
       while @@fetch_status=0
       begin  
         if not exists select * from #temp where no=@no
         begin
           insert into #temp (no,name) values(@no,@name)
         end
         fetch from outwardoperation into @no,@name
       end
       close temptable 
    select a.No,b.name,a.tatal
    from (select no,tatal=sum(num) from yourtable group by No) a,#temp b
    where a.no=b.no
      

  3.   

    很简单: select v1.No ,(select top 1 u1.Name From Person u1 Where u1.No = v1.No) as Name, sum(v1.num) as num 
     from Person v1
     group by v1.no
      

  4.   

    果然高手
    thank's very much!
      

  5.   

    select a.no,b.name,a.sum 
        from (select no,sum(num) as sum from person group by no) as a
             inner join person as b
             on a.no=b.no
    凑个热闹。呵呵