34,590
社区成员
发帖
与我相关
我的任务
分享
with table1(id,起点,目的地) as(
select 1,'北京','上海' union all
select 2,'北京','哈尔滨' union all
select 3,'广州','深圳' union all
select 4,'广州','上海' union all
select 5,'上海','广州' union all
select 6,'上海','北京'
),
tb as(
select id,起点,目的地 from(
select *,row=row_number()over(partition by 起点,目的地 order by getdate())from(
select * from table1 union all
select id,目的地,起点 from table1)t
)tt where row=1
),
tbfirst as ( select top 1 起点,COUNT(起点) as shuliang from tb group by 起点
having COUNT(起点)=1),
source as(
select * from tb where 目的地=(select 起点 from tbfirst)
union all
select tb1.* from tb tb1,source s1 where tb1.目的地=s1.起点 and tb1.起点!=s1.目的地
)
select id=row_number()over(order by getdate()),目的地 from(
select 目的地 from source union all
select 起点 from(
select *,row=row_number()over(order by getdate()) from source)t where row=(select max(row) from (select *,row=row_number()over(order by getdate()) from source)tt)
)ttt
select * from tb where 目的地='哈尔滨'
能不能再帮我修改一下,因为“哈尔滨”和“深圳”是认为判断的节点,能不能给他换成函数,让程序自己判断路由的起始点或终点,再次感谢!