读书人

一个 分组求最大值的有关问题 哪位哥哥

发布时间: 2012-02-27 10:00:22 作者: rapoo

一个 分组求最大值的问题 哪位哥哥姐姐帮忙看下 谢谢了
number cxtime //字段名
69012358974532007-02-03 13:34:51.000
69012358974532007-02-03 13:42:39.000
69045897542152007-02-03 13:43:02.000
69010283000562007-02-03 13:43:20.000
69022650510152007-02-03 13:46:21.000
69012358944542007-02-03 13:48:37.000
69045867854122007-02-03 13:49:41.000
69258745125672007-02-03 13:50:01.000
69025896471232007-02-03 13:51:38.000
69145875621452007-02-03 13:51:54.000
69012358974562007-02-05 10:47:36.000
69012358974552007-02-05 10:51:15.000
69207612002322007-02-05 10:52:51.000
69215555816812007-02-05 10:53:12.000
69215689741232007-02-05 10:53:31.000
69211685555812007-02-05 10:53:52.000
69044420001132007-02-05 11:05:56.000
69056894561232007-02-05 11:06:17.000
69058475681232007-02-05 11:06:34.000
69024020303922007-02-05 11:06:54.000
69178780004592007-02-05 11:07:11.000
69145875621452007-02-05 11:07:27.000
69045867854122007-02-08 18:34:00.000
69045867854122007-02-08 19:27:00.000
69045867854122007-02-09 10:05:00.000 //工 25条记录
郁闷还是这个问题 上次哪位大哥给的答案 我怎么弄不出来 只好 重发一次
我现在想要 number 字段不重复 而且 只取重复记录中的最新时间的那一条 得出结果应该是 :(备注:顺序不论)

number cxtime //字段名

69012358974532007-02-03 13:42:39.000
69010283000562007-02-03 13:43:20.000
69022650510152007-02-03 13:46:21.000
69012358944542007-02-03 13:48:37.000
69045867854122007-02-03 13:49:41.000
69258745125672007-02-03 13:50:01.000
69025896471232007-02-03 13:51:38.000
69012358974562007-02-05 10:47:36.000
69012358974552007-02-05 10:51:15.000
69207612002322007-02-05 10:52:51.000
69215555816812007-02-05 10:53:12.000
69215689741232007-02-05 10:53:31.000
69211685555812007-02-05 10:53:52.000
69044420001132007-02-05 11:05:56.000
69056894561232007-02-05 11:06:17.000
69058475681232007-02-05 11:06:34.000
69024020303922007-02-05 11:06:54.000
69178780004592007-02-05 11:07:11.000
69145875621452007-02-05 11:07:27.000
69045867854122007-02-09 10:05:00.000 //共 20条记录
怎么用SQL语句实现啊 不行的话 存储过程 也行 谢谢各位 哥哥姐姐了


[解决办法]
if object_id( 't ') is not null
drop table t
go
create table t(number varchar(50),cxtime datetime)
insert t select '6901235897453 ', '2007-02-03 13:34:51.000 '
union all select '6901235897453 ', '2007-02-03 13:42:39.000 '
union all select '6904589754215 ', '2007-02-03 13:43:02.000 '
union all select '6901028300056 ', '2007-02-03 13:43:20.000 '
union all select '6902265051015 ', '2007-02-03 13:46:21.000 '
union all select '6901235894454 ', '2007-02-03 13:48:37.000 '
union all select '6904586785412 ', '2007-02-03 13:49:41.000 '


union all select '6925874512567 ', '2007-02-03 13:50:01.000 '
union all select '6902589647123 ', '2007-02-03 13:51:38.000 '
union all select '6914587562145 ', '2007-02-03 13:51:54.000 '
union all select '6901235897456 ', '2007-02-05 10:47:36.000 '
union all select '6901235897455 ', '2007-02-05 10:51:15.000 '
union all select '6920761200232 ', '2007-02-05 10:52:51.000 '
union all select '6921555581681 ', '2007-02-05 10:53:12.000 '
union all select '6921568974123 ', '2007-02-05 10:53:31.000 '
union all select '6921168555581 ', '2007-02-05 10:53:52.000 '
union all select '6904442000113 ', '2007-02-05 11:05:56.000 '
union all select '6905689456123 ', '2007-02-05 11:06:17.000 '
union all select '6905847568123 ', '2007-02-05 11:06:34.000 '
union all select '6902402030392 ', '2007-02-05 11:06:54.000 '
union all select '6917878000459 ', '2007-02-05 11:07:11.000 '
union all select '6914587562145 ', '2007-02-05 11:07:27.000 '
union all select '6904586785412 ', '2007-02-08 18:34:00.000 '
union all select '6904586785412 ', '2007-02-08 19:27:00.000 '
union all select '6904586785412 ', '2007-02-09 10:05:00.000 '
go
select * from t
---我现在想要 number 字段不重复 而且 只取重复记录中的最新时间的那一条 得出结果应该是
select number,max(cxtime) from t group by number order by number

读书人网 >SQL Server

热点推荐