日期:2014-05-17  浏览次数:20451 次

怎么去筛选重复(针对某列不是行)中最新的一条数据(最近时间的)?
数据如下:

1         2            巢湖公安局     2013-02-04 14:43:56.000          110

2         7            巢湖公安局     2013-02-05 10:01:47.000          120

3         9            合肥公安局     2013-01-30 16:30:40.000          180

4         10           合肥公安局     2013-01-28 16:30:06.000          170


筛选出的结果应该是,最新一条不重复(公安局的)记录

2         7            巢湖公安局     2013-02-05 10:01:47.000          120
3         9            合肥公安局     2013-01-30 16:30:40.000          180

------解决方案--------------------
select * from tb t where 时间=(select max(时间) from tb where 公安局=t.公安局)

------解决方案--------------------
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (a int,b int,c nvarchar(10),d datetime,e int)
insert into [TB]
select 1,2,'巢湖公安局','2013-02-04 14:43:56.000',110 union all
select 2,7,'巢湖公安局','2013-02-05 10:01:47.000',120 union all
select 3,9,'合肥公安局','2013-01-30 16:30:40.000',180 union all
select 4,10,'合肥公安局','2013-01-28 16:30:06.000',170

select * from TB where not exists(select 1 from TB A where A.c=TB.c and A.d <TB.d)

/*
a b c d e
1 2 巢湖公安局 2013-02-04 14:43:56.000 110
4 10 合肥公安局 2013-01-28 16:30:06.000 170*/