有以下数据
:
ty tyname 人员 数量
a a1 me 3
a a2 me 1
b b1 you 1
b b2 you 2
b b3 you 1
···
···
···要求得到以下结果:(即是按人员分组,每人只读取数量最大的那条记录)
ty tyname 人员 数量
a a1 me 3
b b2 you 2
···
···
···
解决方案 »
- 想插入一列xian_name 具体怎么写
- (初学者)关于数据库与应用程序的小问题
- mssql调用Linux下的mysql.寻求技术指导[付费请教]
- 求下面SQL的写法
- 请教高手:如果用Sql取第10-20条数据?
- 关于SQL 2000数据库恢复的问题!
- 如何在SQLSERVER2000的一个函数中定义一个常量啊?
- 为什么我的查询分析器没用了。
- 我要在sql server 2000中,在指定时间点对某一表进行update操作,该如何作?
- !!!急!!!SQLserver 2000为什么不能在XPprofession下安装服务器组件??
- SQL语句 赋值
- [Microsoft][ODBC SQL Server Driver]连接占线导致另一个 hstmt 怎么解决?
group by ty,tyname,人员,出来的结果肯定不对的拉。要求是按人员分组,每人只读取数量最大的那条记录
where not exsits(select 1 from tb where 人员=a.人员 and 数量>a.数量)
where not exists(select 1 from tb where 人员=a.人员 and 数量>a.数量)
--or
select * from tb a
where 数量=(select max(数量) from tb where 人员=a.人员)
use tempdb;
/*
create table tb
(
ty nvarchar(10) not null,
tyname nvarchar(10) not null,
人员 nvarchar(10) not null,
数量 int not null
);
insert into tb(ty,tyname,人员,数量)
values
('a','a1','me',3),
('a','a2','me',1),
('b','b1','you',1),
('b','b2','you',2),
('b','b3','you',1);
*/
select t.ty,t.tyname,t.人员,t.数量
from
(
select *,
ROW_NUMBER() over(partition by tb.ty order by tb.数量 desc) as [sort]
from tb
) as t
where t.sort = 1;
from
(
select *,
ROW_NUMBER() over(partition by tb.人员 order by tb.数量 desc) as [sort]
from tb
) as t
where t.sort = 1;