table
图表大致如下:value    Group
12         1
13         1
14         2
14         2
15         3
15         3
希望能够  先按GROUP字段求Value平均数。然后再把所有的已求得的平均数中挑选出最大值和最小值。

解决方案 »

  1.   


    SELECT  TOP 1 [GROUP],AVG([VALUE])AS [VALUE] 
    FROM TB 
    GROUP BY [GROUP] 
    ORDER BY AVG([VALUE])UNION ALLSELECT  TOP 1 [GROUP],AVG([VALUE])AS [VALUE] 
    FROM TB 
    GROUP BY [GROUP] 
    ORDER BY AVG([VALUE]) DESC
      

  2.   

    select max(value) ma,min(value) mi
    FROM 
    (
    select [Group], avg(value) value
    from [Table]
    group by [Group]
    )aa
      

  3.   


    select Min(aa.Value) as minValue,Max(aa.Value) as maxValue  from 
    (
    select [group]
    ,AVG(value)as Value from table2 group by [group]
    ) aa
      

  4.   

    select
     max(value),min(value)
    from 
    (
    select
     [Group], avg(value) value
    from
     tb
    group by
     [Group]
    )t
      

  5.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([value] int,[Group] int)
    insert [tb]
    select 12,1 union all
    select 13,1 union all
    select 14,2 union all
    select 14,2 union all
    select 15,3 union all
    select 15,3
    --
    --select * from [tb];
    with cte1 as
    (
    SELECT    [GROUP],AVG([VALUE])AS [VALUE] 
    FROM TB 
    GROUP BY [GROUP] 
     )select * from cte1 c where not exists(select 1 from cte1 where [VALUE]>c.[VALUE] )
    union all
    select * from cte1 c where not exists(select 1 from cte1 where [VALUE]<c.[VALUE] )
    /*
    GROUP       VALUE
    ----------- -----------
    3           15
    1           12
    */
      

  6.   


    select max(value) as [max],min(value) as [min]
    FROM 
    (
    select [Group], avg(value) value
    from [Table]
    group by [Group]
    ) t