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
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
解决方案 »
- 怎么将一列的全角转为半角sqlserver2005/2008
- 在一个字段里,找出某个字符串(字符)的个数
- 一个排序的问题,不知道可不可以使用ORDERY BY完成
- sql server 2000运行太慢!!!!!!!
- 关于SQL2000的时间查询问题
- trainlist表 是trains表的子集吧,有存在价值么??? 表的设计我是看不懂了
- dbo.sysobject的select权限被拒绝(求助,在线等)
- 怎样发布基于Microsoft SQL Server的程序,谢谢各位高手,快来解决,本人急!
- 多人开发数据库怎样进行版本控制和管理?
- 请问大家:我用的是SWQ SERVER数据库的,大家劝我用VB来开发数据库的,不过我喜欢JAVA请问大家我可以用JAVA来开发数据库并到到用VB开发达
- 如何得到這樣的表?
- 如何写一个SQL: 将结果集导入到另一张表 但忽略联合主键重复的纪录
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
(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
第一步:按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