例如:
序号 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本人实在想不出来,请各位高手帮忙,谢谢。
序号 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本人实在想不出来,请各位高手帮忙,谢谢。
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字段
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字段
from 表
group by A字段,B字段
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
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
*/
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
(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字段
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 行)