select distinct type,  advance
from titles
order by type
compute avg( advance) by type
检索结果是:
type         advance               
------------ --------------------- 
business     5000.0000
business     10125.0000             avg
             =====================
             7562.5000
type         advance               
------------ --------------------- 
mod_cook     .0000
mod_cook     15000.0000             avg
             =====================
             7500.0000
为什么同样的type会出现,而使用
select type, distinct  advance
from titles
order by type
compute avg( advance) by type
会出现问题,请问为什么?谢谢!

解决方案 »

  1.   

    select * from 
    (select distinct type as type,  advance from titles) t
    order by type
    compute avg( advance) by type
      

  2.   

    你要什么结果?
    select type ,avg( advance) 
    from titles
    group by type
    的结果符合你要求吗
      

  3.   

    type相同,但是advance不同,当然会出来了
      

  4.   

    /*引用*/
    使用Compute by子句和行统计函数(count,sum,max,avg,min等),可以统计排序中结果完全相同的列,统计值作为查询结果以附加行的形式显示,语法:Compute avg|count|max|min|sum by 表达式1、举一个例子比如有一个数据表:表名为person,三个字段分别为 name,age,countryselect name,country,age from person where ?? order by a compute sum(age),max(age),min(age) by country这个语句在查询时将数据按照country分组,然后分别显示每组的详细信息和统计信息。结果可能如下:name                  country                        age张三                  中国                        16
    李四                  中国                        21
    王五                  中国                        24sum      max      min61            24        16=================================name                  country                        age泰森                     美国                         20
    布什                     美国                         24
    盖茨                     美国                         25sum      max      min69           25        20=============================================name                  country                        age妓子                  日本                           12
    完犊子                日本                           14
    猪生逆子              日本                           15sum      max      min41          15        12我们很容易看出她其实是在一个查询结果中包含了三个子查询(根据country不同而分的组),每个子查询又包含两个子查询(一个是详细信息,一个是统计信息)2、compute by 子句的规则:(1)不能将distinct与行统计函数一起使用(2)compute ??? by 子句中 ???出的列必须出现在选择列表中(3)不能在含有compute by 子句的语句中使用select into 子句,因为包括compute 子句的语句会产生不规则的行。(4)如果使用了compute by子句,则必须使用order by 子句, 而且compute by子句中的列必须包含在order by 子句中,并且对列的前后顺序和起始项都要一致(说白了compute by子句中的列必须是order by子句中列表的全部,或者前边的连续几个)。(5)如果compute 省略了 by ,则order by 也可以省略(6)如果compute by 子句包含多列时,会将一个组(第一个列分的组)分成若干个子组(利用后面的列),并对每层子组进行统计。(7)使用多个compute by子句时,会分别按不同的组统计出结果。详细信息还是按照正常的第一个分组方式显示。(8)compute by 子句中可以使用多个统计函数,他们互不影响(9)compute by 子句中可以不包含by ,而只用compute  此时不对前面信息分组,而只对全部信息进行统计。