日期:2014-05-18  浏览次数:20562 次

如何提取最大值的一行,求助!!!!!!!!!!急急急!!!!!!1
例如提取count_num中最的值的一行,即提取出来的一行应为  
2 2005-08-07 00:00:00.000 以及2 2005-09-12 00:00:00.000

count_num end_time
1 2005-08-06 00:00:00.000
2 2005-08-07 00:00:00.000
2 2005-09-12 00:00:00.000
1 2005-12-21 00:00:00.000


------解决方案--------------------
SQL code

--> 测试数据:[test]
if object_id('[test]') is not null 
drop table [test]
create table [test](
[count_num] int,
[end_time] datetime
)
insert [test]
select 1,'2005-08-06 00:00:00.000' union all
select 2,'2005-08-07 00:00:00.000' union all
select 2,'2005-09-12 00:00:00.000' union all
select 1,'2005-12-21 00:00:00.000'


select * from test a
where a.count_num=(select MAX(b.count_num) from test b)
/*
count_num    end_time
2    2005-08-07 00:00:00.000
2    2005-09-12 00:00:00.000
*/

------解决方案--------------------
select count_num ,end_time from 表 having count_num =max(count_num) group by count_num