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

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


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

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

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

其他的不变。

求大神!!如何搞???

...全文
230 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匹配
内容概要:本文围绕“可再生能源发电与电动汽车的协同调度策略”展开硕士论文级别的研究与复现工作,基于Matlab平台构建了高比例可再生能源接入背景下电力系统与电动汽车(EV)之间的多时间尺度协同优化模型。研究重点在于应对风光发电不确定性,充分利用电动汽车作为移动储能单元的灵活充放电能力(含V2G技术),实现电网削峰填谷、提升新能源消纳效率与系统运行经济性。文中综合考虑了异质电动车集群特性、V2G过程中的损耗约束以及随机因素影响,采用如深度确定性策略梯度(DDPG)强化学习、混合整数线性规划(MILP)等先进优化算法进行求解,并通过基准模型对比验证了所提自适应调度策略在提升电网稳定性、降低运行成本方面的有效性。; 适合人群:具备一定电力系统基础知识、优化理论背景及Matlab编程能力的研究生、科研人员,以及从事新能源并网、智能电网调度、车网互动(V2G)等方向的工程技术人员。; 使用场景及目标:①学习并复现已发表高水平硕士论文中的协同调度建模方法与仿真流程;②掌握将深度强化学习(如DDPG)应用于虚拟电厂、电动汽车集群调度等复杂非线性决策问题的技术路径;③为开展新能源消纳、需求侧响应、微电网优化运行等相关课题的研究提供可复用的代码框架与创新思路参考。; 阅读建议:建议读者结合文中提供的完整Matlab代码与技术文档,深入理解模型构建逻辑、算法实现细节与参数设置依据,优先在Simulink/Matlab环境中运行并调试仿真案例,通过调整场景参数与算法超参,探究不同策略对调度效果的影响,从而深化对协同调度机制与智能优化方法应用的理解。

22,296

社区成员

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

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