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

怎样过滤连续重复的数据呢?解决方法

2012-03-19 
怎样过滤连续重复的数据呢?ID经度纬度1116.136.12116.236.13116.236.14116.236.15116.336.16116.236.1我想

怎样过滤连续重复的数据呢?
ID 经度 纬度
1 116.1 36.1
2 116.2 36.1
3 116.2 36.1
4 116.2 36.1
5 116.3 36.1
6 116.2 36.1


我想要他出来
1 116.1 36.1
2 116.2 36.1
5 116.3 36.1
6 116.2 36.1

[解决办法]

SQL code
select min(id) id,经度,纬度from tbgroup by 经度,纬度
[解决办法]
0.0 我看错了,1楼不行。

SQL code
;with ach as(    select *,rid=row_number() over (order by getdate()),             pid=row_number() over (partition by 经度,纬度 order by id)    from tb)select *from ach twhere not exists (select 1 from ach where rid-pid=t.rid-t.pid and id < t.id)
[解决办法]
SQL code
--> 测试数据:[TBL]goif object_id('[TBL]') is not null drop table [TBL]gocreate table [TBL]([ID] int,[经度] numeric(4,1),[纬度] numeric(3,1))goinsert [TBL]select 1,116.1,36.1 union allselect 2,116.2,36.1 union allselect 3,116.2,36.1 union allselect 4,116.2,36.1 union allselect 5,116.3,36.1 union allselect 6,116.2,36.1;WITH TAS(SELECT *,ROW_NUMBER()OVER(ORDER BY GETDATE()) AS NUM,ROW_NUMBER()OVER(PARTITION BY [经度],[纬度] ORDER BY [ID] ) AS [ORDER]FROM [TBL])SELECT [ID],[经度],[纬度] FROM T WHERE [ORDER]=1UNIONSELECT [ID],[经度],[纬度] FROM T WHERE ID-[ORDER]<>1/*ID    经度    纬度1    116.1    36.12    116.2    36.15    116.3    36.16    116.2    36.1*/ 

热点排行