34,873
社区成员
发帖
与我相关
我的任务
分享with table1(起点,目的地,名称) as(
select '广州','深圳','哈深' union all
select '北京','哈尔滨','哈深' union all
select '北京','上海','哈深' union all
select '广州','上海' ,'哈深'union all
select '上海','广州','哈深' union all
select '沈阳','大连','哈大' union all
select '哈尔滨','沈阳','哈大' union all
select '上海','北京','哈深' union all
select '沈阳','哈尔滨','哈大' union all
select '大连','沈阳','哈大'union all
select '哈尔滨','长春','京哈'union all
select '长春','哈尔滨','京哈'union all
select '长春','沈阳','京哈'union all
select '沈阳','长春','京哈'union all
select '沈阳','北京','京哈'union all
select '沈阳','北京','京哈'union all
select '兰州','张掖','兰新'union all
select '张掖','兰州','兰新'union all
select '张掖','乌鲁木齐','兰新'union all
select '乌鲁木齐','张掖','兰新'
),
tb as(
select 起点,目的地,名称 from(
select *,row=row_number()over(partition by 起点,目的地 order by getdate())from(
select * from table1 union all
select 目的地,起点,名称 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()),名称,(select top 1 起点 from source s2 where s2.名称=s1.名称)+(select '-'+目的地 from source s2 where s2.名称=s1.名称 for xml path('')) 途经 from source s1 group by 名称with table1(起点,目的地,名称) as(
select '广州','深圳','陈冠希' union all
select '北京','哈尔滨','陈冠希' union all
select '北京','上海','陈冠希' union all
select '广州','上海' ,'陈冠希'union all
select '上海','广州','陈冠希' union all
select '上海','北京','陈冠希' union all
select '广州','深圳','张柏芝' union all
select '北京','哈尔滨','张柏芝' union all
select '北京','上海','张柏芝' union all
select '广州','上海' ,'张柏芝'union all
select '上海','广州','张柏芝' union all
select '广州','深圳','阿娇' union all
select '北京','哈尔滨','阿娇' union all
select '北京','上海','阿娇' union all
select '广州','上海' ,'阿娇'union all
select '上海','广州','阿娇' union all
select '北京','上海','我' union all
select '上海','北京','我' union all
select '广州','上海' ,'我'union all
select '上海','广州','我' union all
select '北京','上海','你' union all
select '上海','北京','你' union all
select '广州','上海' ,'你'union all
select '上海','广州','你' union all
select '沈阳','大连','谢霆锋' union all
select '哈尔滨','沈阳','谢霆锋' union all
select '沈阳','哈尔滨','谢霆锋' union all
select '大连','沈阳','谢霆锋'union all
select '哈尔滨','长春','舒淇'union all
select '长春','哈尔滨','舒淇'union all
select '长春','沈阳','舒淇'union all
select '沈阳','长春','舒淇'union all
select '沈阳','北京','舒淇'union all
select '长春','沈阳','王姐'union all
select '沈阳','长春','王姐'union all
select '沈阳','北京','王姐'union all
select '长春','沈阳','捅主任'union all
select '沈阳','长春','捅主任'union all
select '沈阳','北京','捅主任'union all
select '长春','沈阳','郭德纲'union all
select '沈阳','长春','郭德纲'union all
select '沈阳','北京','郭德纲'union all
select '长春','沈阳','于谦'union all
select '沈阳','长春','于谦'union all
select '沈阳','北京','于谦'union all
select '兰州','张掖','二蛋'union all
select '张掖','兰州','二蛋'union all
select '张掖','乌鲁木齐','二蛋'union all
select '乌鲁木齐','张掖','二蛋' union all
select '兰州','张掖','成龙'union all
select '张掖','兰州','成龙'union all
select '张掖','乌鲁木齐','成龙'union all
select '乌鲁木齐','张掖','成龙'
)
select * from table1
把原表改了改,用hjywyj的语句得到下面结果:
名称 path
阿娇 广州-上海-北京-哈尔滨
陈冠希 哈尔滨-北京
成龙 兰州-张掖
二蛋 乌鲁木齐-张掖-兰州
你 北京-上海-广州
舒淇 沈阳-长春-哈尔滨
捅主任 长春-沈阳
谢霆锋 哈尔滨-沈阳-大连
于谦 北京-沈阳
而我现在需要的结果集如下:
名称 path
陈冠希 哈尔滨-北京-上海-广州-深圳
张柏芝 哈尔滨-北京-上海-广州-深圳
阿娇 哈尔滨-北京-上海-广州-深圳
我 北京-上海-广州
你 北京-上海-广州
谢霆锋 哈尔滨-沈阳-大连
舒淇 北京-沈阳-长春-哈尔滨
王姐 北京-沈阳-长春
郭德纲 北京-沈阳-长春
于谦 北京-沈阳-长春
二蛋 乌鲁木齐-张掖-兰州
成龙 乌鲁木齐-张掖-兰州
意思是根据人的名字 找出他所行走的路线,这个对于你们来说很简单,但对于我来说真的是超难,辛苦了各位,尤其是hjywyj,版主也帮忙看看吧,谢谢诸位!
(起始点和目的地顺序可颠倒:如 北京-沈阳-长春 可以为 长春-沈阳-北京)
亲,要不你发个大贴,一直问道你没问题你再结贴吧。我旁观都累了。