求助一个SQL问题!!难住了!

敌敌畏耶 2019-03-18 04:08:45


如图:
如果出现连续三条及三条以上一样(Longitude和Latitude一样)的数据的时候,只取开始出现和最后出现的数据。

比如:Id为208到225是一样的,则取208和225这两条、

id为241到245是一样的,则取241和245这两条。

其他的不变。

求大神!!如何搞???

...全文
188 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
RINK_1 2019-03-19
  • 打赏
  • 举报
回复

WITH CTE
AS
(SELECT *,COUNT(*) OVER (PARTITION BY LONGITUDE,LATITUDE) AS QTY,
ROW_NUMBER() OVER (PARTITION BY LONGITUDE,LATITUDE ORDER BY ID) AS SEQ_1, 
ROW_NUMBER() OVER (PARTITION BY LONGITUDE,LATITUDE ORDER BY ID DESC) AS SEQ_2
FROM T)

SELECT * FROM CTE 
WHERE QTY>=3 AND (SEQ_1=1 OR SEQ_2=1)
leo_lesley 2019-03-19
  • 打赏
  • 举报
回复


create table t (id int , Longitude int , Latitude int )
go

insert t select 435 ,1 ,2
union select 436 ,1 ,2
union select 437 ,1 ,3
union select 438 ,1 ,3
union select 439 ,1 ,3
union select 569 ,1 ,3
union select 577 ,1 ,3
union select 581 ,1 ,4
union select 583 ,1 ,4
union select 584 ,1 ,4
union select 587 ,1 ,5
union select 589 ,1 ,6
union select 591 ,1 ,6
union select 594 ,1 ,6
union select 596 ,1 ,6
union select 598 ,1 ,6
union select 7135 ,1 ,6
union select 7136,1 ,3
union select 7137 ,1 ,3
union select 7138,1 ,3
union select 7139,1 ,3
union select 7140 ,1 ,4
union select 7141,1 ,4
union select 7142,1 ,4


go

-- 试试这样
with cet
as(
select id2 - id1 flag ,min(id) id_1 , max(id) id_2
from (select id1 = row_number() over(partition by Latitude order by id) ,id2 = row_number() over( order by id) ,* from t ) a
group by id2 - id1
)
select distinct t.* from t join cet b on t.id = b.id_1 or t.id = b.id_2

go
drop table t


敌敌畏耶 2019-03-18
  • 打赏
  • 举报
回复
引用 4 楼 好奇都是要学的 的回复:
select distinct * from ( select * from t where id=(select MIN(id) from t b where t.Longitude=b.Longitude and t.Latitude=b.Latitude) union all select * from t where id=(select max(id) from t b where t.Longitude=b.Longitude and t.Latitude=b.Latitude)) a order by id 写法有很多,看你怎么思考, 我这个还可以优化下
如果是这种数据呢?

insert t select 435 ,1 ,2
union select 436 ,1 ,2
union select 437 ,1 ,3
union select 438 ,1 ,3
union select 439 ,1 ,3
union select 569 ,1 ,3
union select 577 ,1 ,3
union select 581 ,1 ,4
union select 583 ,1 ,4
union select 584 ,1 ,4
union select 587 ,1 ,5
union select 589 ,1 ,6
union select 591 ,1 ,6
union select 594 ,1 ,6
union select 596 ,1 ,6
union select 598 ,1 ,6
union select 7135 ,1 ,6
union select 7136,1 ,3
union select 7137 ,1 ,3
union select 7138,1 ,3
union select 7139,1 ,3
union select 7140 ,1 ,4
union select 7141,1 ,4
union select 7142,1 ,4
敌敌畏耶 2019-03-18
  • 打赏
  • 举报
回复
引用 2 楼 leo_lesley 的回复:



create table t (id int , Longitude int , Latitude int )
go
insert t 
	  select 435 ,1 ,2
union select 436 ,1 ,2
union select 437 ,1 ,3
union select 438 ,1 ,3
union select 439 ,1 ,3
union select 569 ,1 ,3
union select 577 ,1 ,3
union select 581 ,1 ,4
union select 583 ,1 ,4
union select 584 ,1 ,4
union select 587 ,1 ,5
union select 589 ,1 ,6
union select 591 ,1 ,6
union select 594 ,1 ,6
union select 596 ,1 ,6
union select 598 ,1 ,6
union select 7135 ,1 ,6


go

select distinct * from t a join (select Longitude,Latitude,min(id) id1 , max(id) id2 from t group by Longitude,Latitude ) b on a.id = b.id1 or a.id = b.id2

go
drop table t 


/*
id	Longitude	Latitude	Longitude	Latitude	id1	id2
435	1	2	1	2	435	436
436	1	2	1	2	435	436
437	1	3	1	3	437	577
577	1	3	1	3	437	577
581	1	4	1	4	581	584
584	1	4	1	4	581	584
587	1	5	1	5	587	587
589	1	6	1	6	589	7135
7135	1	6	1	6	589	7135
*/

这个还有问题。。。。 如果数据是这样的呢?

insert t select 435 ,1 ,2
union select 436 ,1 ,2
union select 437 ,1 ,3
union select 438 ,1 ,3
union select 439 ,1 ,3
union select 569 ,1 ,3
union select 577 ,1 ,3
union select 581 ,1 ,4
union select 583 ,1 ,4
union select 584 ,1 ,4
union select 587 ,1 ,5
union select 589 ,1 ,6
union select 591 ,1 ,6
union select 594 ,1 ,6
union select 596 ,1 ,6
union select 598 ,1 ,6
union select 7135 ,1 ,6
union select 7136,1 ,3
union select 7137 ,1 ,3
union select 7138,1 ,3
union select 7139,1 ,3
union select 7140 ,1 ,4
union select 7141,1 ,4
union select 7142,1 ,4
如果是这种数据的话,出来的结果就不对呀!
  • 打赏
  • 举报
回复
select * from t where t.id in(select min(id) id1 from t group by Longitude,Latitude) or t.id in(select max(id) id1 from t group by Longitude,Latitude)
  • 打赏
  • 举报
回复
select distinct * from ( select * from t where id=(select MIN(id) from t b where t.Longitude=b.Longitude and t.Latitude=b.Latitude) union all select * from t where id=(select max(id) from t b where t.Longitude=b.Longitude and t.Latitude=b.Latitude)) a order by id 写法有很多,看你怎么思考, 我这个还可以优化下
敌敌畏耶 2019-03-18
  • 打赏
  • 举报
回复
引用 2 楼 leo_lesley 的回复:



create table t (id int , Longitude int , Latitude int )
go
insert t 
	  select 435 ,1 ,2
union select 436 ,1 ,2
union select 437 ,1 ,3
union select 438 ,1 ,3
union select 439 ,1 ,3
union select 569 ,1 ,3
union select 577 ,1 ,3
union select 581 ,1 ,4
union select 583 ,1 ,4
union select 584 ,1 ,4
union select 587 ,1 ,5
union select 589 ,1 ,6
union select 591 ,1 ,6
union select 594 ,1 ,6
union select 596 ,1 ,6
union select 598 ,1 ,6
union select 7135 ,1 ,6


go

select distinct * from t a join (select Longitude,Latitude,min(id) id1 , max(id) id2 from t group by Longitude,Latitude ) b on a.id = b.id1 or a.id = b.id2

go
drop table t 


/*
id	Longitude	Latitude	Longitude	Latitude	id1	id2
435	1	2	1	2	435	436
436	1	2	1	2	435	436
437	1	3	1	3	437	577
577	1	3	1	3	437	577
581	1	4	1	4	581	584
584	1	4	1	4	581	584
587	1	5	1	5	587	587
589	1	6	1	6	589	7135
7135	1	6	1	6	589	7135
*/

谢谢大神!!!!
leo_lesley 2019-03-18
  • 打赏
  • 举报
回复



create table t (id int , Longitude int , Latitude int )
go
insert t
select 435 ,1 ,2
union select 436 ,1 ,2
union select 437 ,1 ,3
union select 438 ,1 ,3
union select 439 ,1 ,3
union select 569 ,1 ,3
union select 577 ,1 ,3
union select 581 ,1 ,4
union select 583 ,1 ,4
union select 584 ,1 ,4
union select 587 ,1 ,5
union select 589 ,1 ,6
union select 591 ,1 ,6
union select 594 ,1 ,6
union select 596 ,1 ,6
union select 598 ,1 ,6
union select 7135 ,1 ,6


go

select distinct * from t a join (select Longitude,Latitude,min(id) id1 , max(id) id2 from t group by Longitude,Latitude ) b on a.id = b.id1 or a.id = b.id2

go
drop table t


/*
id Longitude Latitude Longitude Latitude id1 id2
435 1 2 1 2 435 436
436 1 2 1 2 435 436
437 1 3 1 3 437 577
577 1 3 1 3 437 577
581 1 4 1 4 581 584
584 1 4 1 4 581 584
587 1 5 1 5 587 587
589 1 6 1 6 589 7135
7135 1 6 1 6 589 7135
*/

leo_lesley 2019-03-18
  • 打赏
  • 举报
回复
你的图片看不到, 不过可以给你个思路,先分组取最大值和最小值,然后用id匹配

22,297

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧