select name,sum(money) as money,(sum(money)-avg(money1)) as money1 from temp group by name
union all
select '总计',sum(money) as money,(sum(money)-avg(money1)) as money1 from temp

解决方案 »

  1.   

    select name,sum(money) as money,(sum(money)-avg(money1)) as money1 from je group by name
    union
    select '合计:' as name, sum(money) as money,(sum(money)-avg(money1)) as money1 from je 
      

  2.   

    select name,sum(money) as money,(sum(money)-avg(money1)) as money1 from temp group by name with rollup
      

  3.   

    select name,sum(money) as money,(sum(money)-avg(money1)) as money1 from temp group by name
    union
    select '合计:' as name, sum(money) as money,(sum(money)-avg(money1)) as money1 from temp
      

  4.   

    select name,money,money from temp where
       name=(select distion from temp where name='aaa' or name=='bbb')
       and
       money=
      (select count(money) from temp where name='aaa' or name=='bbb')
       and 
       money1=
      (select count(money1) from temp where money1=
      (select money1 from temp where money1=count(money)-count(money1) 
       and
      name="aaa" or name="bbb")) 
      

  5.   

    不懂介绍点初学SQL的好网站吧.谢谢
      

  6.   


    select name=case grouping(name) when 1 then '总计:' else name end
    ,[money]=sum([money])
    ,money1=sum([money]-money1)
    from [temp]
    group by name with rollup
      

  7.   

    可能大家都不太明白我想要得到的表,让我再讲一遍吧。
    目标表中的每一行数据是这样得来的:"money"字段按"name"进行累加,"money1"字段按"name"进行求平均再与"money"累加的和相减(其实money1项中按"name"都会是一样的)
    如:"aaa"的"money"是123+234=357,"money"是357-(11+11)/2=346
      "bbb"的"money"是567+789+678=2034,"money"是2034-(22+22+22)/3=2012 但若用“union all
    select '总计',sum(money) as money,(sum(money)-avg(money1)) as money1 from temp”或“with rollup”都得不到想要的结果
    (得到的是“总计:  2391   2373.4”)
    可能是我的表达不好,请大家原谅,请大家帮忙想想,谢谢!!!
      

  8.   

    select name,sum(money) as money,(sum(money)-avg(money1)) as money1 into t from temp group by name
    go 
    select name,sum(money) as money,(sum(money)-avg(money1)) as money1 from temp group by name
    union all
    select '总计',sum(money) as money,(sum(money1)) as money1 from t
      

  9.   

    楼上的应该可以满足楼主需求了,的确没有必要非得是一个SQL语句
      

  10.   

    我用上面的方法,但显示:"Cannot run SELECT INTO in this database. The database owner must run sp_dboption to enable this option."
    是什么意思,我用的是sql2000 server ,请帮忙解决。
      

  11.   

    sql2000应该不会出现这样的提示吧(我电脑装的是sql2000,但没有你所说的情况,不管我怎样设置sp_dboption 数据库名,'select into/bulkcopy'的值,上面语句都可以运行)?你用的可能是sql7.0,你这样试一下(到sql查询分析器中运行):
    sp_dboption 数据库名, 'select into/bulkcopy',true如果还出错,那你就这样吧,应该不会有问题,只不过效率可能不太好.
    select name,sum(money) as money,(sum(money)-avg(money1)) as money1 from temp group by name
    union all
    select '总计',sum(money) as money,(sum(money1)) as money1 from 
    (
    select name,sum(money) as money,(sum(money)-avg(money1)) as money1 from temp group by name
    )a
      

  12.   

    我觉得好多时候大家总是关注用sql来实现一些比较复杂的显示,其实这不是sql的优势,数据库只是用来存储数据的,而你的显示和处理更多的应该在程序里,而不要把这些交给数据库处理,这不是数据库的优势。使用数据库你考虑更多的应该是如何合理快速的取得数据,然后通过应用程序的处理得到最后结果。比如楼主的问题,其实只是想通过数据库处理就能达到你显示的目的,虽然可能通过sql语句实现(具体怎么通过sql来实现我没有细考虑:),但是考虑到性能,我觉得这样做不是很合理。如果我们通过这个问题,目的只是讨论提高我们的sql能力,我觉得楼主的题目很好;但是就一个应用的实现,我以为不妥。个人观点,请大家指正。