首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > IBM DB2 >

求某字段最大值的一条记录的集合,该如何解决

2012-02-15 
求某字段最大值的一条记录的集合create table testsql (name varchar(10),hours varchar(4),date varchar(

求某字段最大值的一条记录的集合
create table testsql (name varchar(10),hours varchar(4),date varchar(8));
insert into testsql ('张三','8','20080101');
insert into testsql ('张三','10','20080102');
insert into testsql ('张三','9','20080103');
insert into testsql ('张三','7','20080104');
insert into testsql ('李四','7','20080104');
insert into testsql ('李四','1','20080105');
insert into testsql ('李四','2','20080106');
insert into testsql ('李四','3','20080107');

表定义:
TESTSQL表中记录每个员工每天工作的小时数

功能实现:
取出TESTSQL表中每个员工某天工作量最大的一条记录信息,结果期望如下
'张三','10','20080102'
'李四','7','20080104'

不能使用存储过程

[解决办法]
--首先SQL有问题,帮你改下(values)。
create table testsql (name varchar(10),hours varchar(4),date varchar(8));
insert into testsql values('张三','8','20080101');
insert into testsql values('张三','10','20080102');
insert into testsql values('张三','9','20080103');
insert into testsql values('张三','7','20080104');
insert into testsql values('李四','7','20080104');
insert into testsql values('李四','1','20080105');
insert into testsql values('李四','2','20080106');
insert into testsql values('李四','3','20080107');
--建议hours改为数字型好比较大小
--sql
Select t2.* from
 (select name,char(max(int(hours))) as hours from testsql group by name) as t1,
 testsql t2
 where t1.name=t2.name
 and t1.hours=t2.hours

[解决办法]
不明LZ在说什么
[解决办法]

引用楼主 walkwolf 的帖子:
create table testsql (name varchar(10),hours varchar(4),date varchar(8));
insert into testsql ('张三','8','20080101');
insert into testsql ('张三','10','20080102');
insert into testsql ('张三','9','20080103');
insert into testsql ('张三','7','20080104');
insert into testsql ('李四','7','20080104');
insert into testsql ('李四','1','20080105');
insert into testsql ('李四','2','200801…
表定义:
TESTSQL表中记录每个员工每天工作的小时数

功能实现:
取出TESTSQL表中每个员工某天工作量最大的一条记录信息,结果期望如下
'张三','10','20080102'
'李四','7','20080104'

不能使用存储过程

热点排行