例如:
  序号    A字段  B字段  C字段
  1        a     b       1
  2        a     b       2
  3        a     b       3
  4        c     d       4
  5        c     d       5
  6        c     d       6我想得到如下结果(取出C字段最大值的count汇总)  A字段      B字段    C字段  统计(count)     
   a          b        3       1
   c          d        6       1本人实在想不出来,请各位高手帮忙,谢谢。

解决方案 »

  1.   

    SELECT *,COUNT(*)统计
    FROM 
    (SELECT A字段 , B字段 , C字段 FROM TB T WHERE NOT EXISTS(SELECT 1 FROM TB WHERE T.A字段=A字段 AND B字段=T.B字段 AND T.C字段<C字段))AS T
    GROUP BY A字段 , B字段 , C字段
      

  2.   

    select A字段,B字段,C字段=max(C字段),统计=count(max(C字段)) from tb group A字段,B字段
      

  3.   

    会报错,count(max(C字段))
      

  4.   


    select a.A字段,a.B字段,a.C字段,count(1) as '統計' 
    from tb a
    where  not exists(select 1 from tb a.A字段=A字段 and a.B字段=A字段 and C字段>a.C字段)
    group by a.A字段,a.B字段,a.C字段
      

  5.   

    select A字段,B字段,C字段=MAX(c字段) ,COUNT(*)as 统计
    from 表
    group by A字段,B字段
      

  6.   

    SELECT *,统计=COUNT(1)
    FROM (
    SELECT A, B, C 
    FROM TB T 
    WHERE NOT EXISTS
    (
    SELECT 1 FROM TB 
    WHERE T.A=A AND B=T.B AND T.C<C
    )
    ) X
    GROUP BY A, B,C
      

  7.   


    declare @table table (序号 int,A字段 varchar(1),B字段 varchar(1),C字段 int)
    insert into @table
    select 1,'a','b',1 union all
    select 2,'a','b',2 union all
    select 3,'a','b',3 union all
    select 4,'c','d',4 union all
    select 5,'c','d',5 union all
    select 6,'c','d',6select A字段,B字段,max(C字段) as C字段,1 as '统计(count)'  from @table group by A字段,B字段
    /*
    A字段  B字段  C字段         统计(count)
    ---- ---- ----------- -----------
    a    b    3           1
    c    d    6           1
    */
      

  8.   

    declare @tab table (A varchar(1),B varchar(1),C int)
    insert into @tab(A,B,C)
    select 'a','b',1  union 
    select 'a','b',2  union  
    select 'a','b',3  union 
    select 'c','d',4  union 
    select 'c','d',5  union 
    select 'c','d',6 select t2.a,t2.b,t2.c,count(*) as cnt from 
    @tab as t1
    inner join
    (
    select a,b,max(c) as c
    from @tab group by a,b
    ) as t2 
    on t1.a = t2.a and t1.b = t2.b and t1.c = t2.c
    group by t2.a,t2.b,t2.c
    a b c cnt
    a b 3 1
    c d 6 1
      

  9.   

    试试select tb2.A字段,tb2.B字段,tb2.C字段,count(tb2.C字段) 合计 from
    (select A字段,B字段,max(C字段) C字段 from tb group by A字段,B字段) as tb2,tb
    where tb2.A字段=tb.A字段 and tb2.B字段=tb.B字段 and tb2.C字段=tb.C字段 
    group by tb2.A字段,tb2.B字段,tb2.C字段
      

  10.   


    create table #tRet 
    (
    序号 int identity not null,
    A字段 varchar(10) ,
    B字段 varchar(10) ,
    C字段  int
    )insert into #tRet
    select 'a','b',1 
    union all
    select 'a','b',2
    union all
    select 'a','b',3 
    union all
    select 'c','d',4 
    union all
    select 'c','d',5 
    union all
    select 'c','d',6select A字段, B字段, max(C字段) as C字段,1 as [统计(count)]
    from #tRet 
    group by A字段,B字段-- 结果集
    A字段        B字段        C字段         统计(count)   
    ---------- ---------- ----------- ----------- 
    a          b          3           1
    c          d          6           1(所影响的行数为 2 行)