create table t(seq_no int,in_no varchar(10),type char(1),[count] int)
insert into T select 1,'001','A',10
insert into T select 2,'001','B',5
insert into T select 3,'001','C',8
insert into T select 1,'002','A',5
insert into T select 2,'002','A',10
insert into T select 1,'003','A',15
insert into T select 2,'003','B',8
insert into T select 3,'003','A',7  
insert into T select 4,'003','B',10select
    b.type,
    count([b.count])
from
    (select 
         a.*
     from
         t a
     where
         a.seq_no = (select max(seq_no) from t where in_no=a.in_no)) b
group by
    b.type

解决方案 »

  1.   

    更正一下:create table t(seq_no int,in_no varchar(10),type char(1),[count] int)
    insert into T select 1,'001','A',10
    insert into T select 2,'001','B',5
    insert into T select 3,'001','C',8
    insert into T select 1,'002','A',5
    insert into T select 2,'002','A',10
    insert into T select 1,'003','A',15
    insert into T select 2,'003','B',8
    insert into T select 3,'003','A',7  
    insert into T select 4,'003','B',10select
        b.type,
        sum([count])
    from
        (select 
             a.*
         from
             t a
         where
             a.seq_no = (select max(seq_no) from t where in_no=a.in_no)) b
    group by
        b.type
      

  2.   

    select type,count(*) from 
    (select * from tb A inner join 
    (select  in_no,max(seq_no ) as seq_no from tb group by in_no) B
    on A.in_no=B.in_no and A.seq_no=B.seq_no) C
    group by type
      

  3.   

    不好意思~我的意图:
    第一步:按in_no分组得到seq_no最大的记录:
    3        001     C      8
    2        002     A      10
    4        003     B      10
    第二步:按type分组求count的和:
    3        001     C      8
    2        002     A      10
    4        003     B      10