hjywyj 还得麻烦你一下啊,请进,呵呵

hzg98 2013-01-06 09:06:14
请再帮我看一下,昨天你给我的代码,我又增加了一些,出现了一些问题,能不能再帮我看一下,谢谢!
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 名称


检测一下是source那个表有问题,但不知问题出在哪,再帮我看看吧,谢谢你!
...全文
254 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzg98 2013-01-08
  • 打赏
  • 举报
回复
问题终于解决了 ,句子也研究了,十分感谢
  • 打赏
  • 举报
回复
with table1(起点,目的地,名称) as( ...... ) tb as( select distinct 起点,目的地,名称 from( select 起点,目的地,名称 from table1 union all select 目的地,起点,名称 from table1 )t ), cte as( select *,path=cast(起点+'-'+目的地 as varchar(max)) from tb union all select tb1.*,path=cast(tb1.起点+'-'+c.path as varchar(max)) from cte c,tb tb1 where tb1.目的地=c.起点 and tb1.起点!=c.目的地 and tb1.名称=c.名称 ) select 名称,path from( select id=row_number()over(partition by 名称 order by len(path) desc),* from cte )t where id=1 order by 名称
hzg98 2013-01-06
  • 打赏
  • 举报
回复
好吧,亲 这后面的保证是最后一个了,呵呵,很复杂,头都炸了。。。。
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,版主也帮忙看看吧,谢谢诸位! (起始点和目的地顺序可颠倒:如 北京-沈阳-长春 可以为 长春-沈阳-北京)
發糞塗牆 2013-01-06
  • 打赏
  • 举报
回复
亲,要不你发个大贴,一直问道你没问题你再结贴吧。我旁观都累了。
  • 打赏
  • 举报
回复
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 ), cte as( select *,path=cast(目的地+'-'+起点 as varchar(max)) from tb union all select tb1.*,path=cast(c.path+'-'+tb1.起点 as varchar(max)) from cte c,tb tb1 where tb1.目的地=c.起点 and tb1.起点!=c.目的地 and tb1.名称=c.名称 ) select 起点,目的地,名称,path from( select id=row_number()over(partition by 名称 order by len(path) desc),* from cte )t where id=1

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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