首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

公交转车路线sql写法解决方案

2012-02-19 
公交转车路线sql写法线路运行区间和时间沿途站点游2线下行灵谷寺公园8:40-17:30雨花台南门7:40-16:30灵谷

公交转车路线sql写法
线路   运行区间和时间   沿途站点  
游2线下行   灵谷寺公园8:40-17:30
雨花台南门7:40-16:30  
灵谷寺公园-水榭-中山陵-海底世界-明孝陵-石象路-前湖-卫桥(西)-中山门-明故宫-解放路-总统府-大行宫南站-杨公井-夫子庙-三山街-金沙井-长乐路-中华门内-雨花台-雨花新村-共青团路-雨花台南门

 
14路下行   所街5:40-23:00
双龙街西站5:10-22:30  
所街-湖西街(应天大街)-应天大街西-黄山路-泰山路-南苑新村-彩虹苑-集庆门大桥-集庆门-殷高巷-长乐路-箍桶巷-马道街-江宁路-大树城-养虎巷-土城头-土城头南站-城头城-卡子门-夹岗-双龙街西站

 
乘车路线:由   [夫子庙]   坐   <游2线下行>   到   [长乐路]   换乘   <14路下行>   到达   [江宁路]  

============================================================================
公交查询,比如从夫子庙到江宁     ,有的可以直达,有的需要转车,按照普通做法似乎要写很多循环来判断,特别是需要转车,请大家提提有没有什么好的写的方法,谢谢,分不够再加

[解决办法]
if exists(select * from sysobjects where id=object_id( 'bus_proc '))
drop proc bus_fun
GO
if exists(select * from sysobjects where id=object_id( 'bus '))
drop table bus
GO

create table Bus(id int identity(1,1) --自增ID
,sid int--车站ID
,lid int)--公交车ID
insert into Bus
select1,1
union all select 3,1
union all select 5,1
union all select2,3
union all select3,3
union all select4,5
union all select1,5
union all select2,7
union all select4,7
union all select6,7
union all select3,9
union all select1,9
union all select7,7
union all select7,10
union all select12,10
union all select12,11
union all select28,11
union all select 10,1
union all select 10,9
union all select 11,9
union all select 14,9
GO

create proc Bus_fun(@sid1 int,@sid2 int,@lev int,@str varchar(8000)= ' ' output)
as
--BY 华裔大魔王
declare @lid int,@buscount int
select @lid = 0 , @lev=0
select @buscount=count(lid) from (select distinct lid from bus ) tb
create table #(Bid int identity(1,1),车站 int,公交 int,lev int)
while exists(select 1 from (select distinct 公交 from #) tb having count(1) <@buscount)
begin
if @lev=0
insert into # (车站 ,公交 ,lev )
select sid,lid,@lev from bus a where exists(select * from bus where sid=@sid1 and lid=a.lid) order by lid,sid
else
insert into # (车站 ,公交 ,lev )
select sid,lid,@lev from bus sb where exists(select * from bus a where exists(select * from # where 车站=a.sid and lev=@lev-1) and not exists (select * from # where 公交=a.lid) and lid = sb.lid)
if @@rowcount=0
goto ERR
if exists(select * from # a where 车站=@sid2 and lev = @lev)
begin
while @lev> =0
begin
select top 1 @lid=公交,@str= '-> 换乘-> 公交 '+cast(公交 as varchar(10))+ '路-> 在 '+cast(车站 as varchar(10)) + '站下车 '+@str from # where 车站=@sid2 and lev=@lev
select top 1 @sid2=车站 from # a where lev=@lev-1 and exists(select * from # where lev=@lev and 车站=a.车站 and 公交=@lid) select @lev=@lev-1
end
select @str=stuff(@str,1,3, ' ')
goto BUSEND
end
select @lev=@lev+1
end
ERR:
select '无公交线路 '
BUSEND:
drop table #
GO

set nocount on
declare @bus varchar(8000)
select @bus= ' '
exec Bus_fun 1,6,0,@bus output
if @bus <> ' '
select @bus



[解决办法]
--參考
/***********邹老大*************/

CREATE TABLE T_Line(
ID nvarchar(10), --公交线路号
Station nvarchar(10), --站点名称
Orders int) --行车方向(通过它反应每个站的上一个、下一个站)
INSERT T_Line
SELECT N '8路 ' ,N '站A ',1 UNION ALL
SELECT N '8路 ' ,N '站B ',2 UNION ALL


SELECT N '8路 ' ,N '站C ',3 UNION ALL
SELECT N '8路 ' ,N '站D ',4 UNION ALL
SELECT N '8路 ' ,N '站J ',5 UNION ALL
SELECT N '8路 ' ,N '站L ',6 UNION ALL
SELECT N '8路 ' ,N '站M ',7 UNION ALL
SELECT N '20路 ' ,N '站G ',1 UNION ALL
SELECT N '20路 ' ,N '站H ',2 UNION ALL
SELECT N '20路 ' ,N '站I ',3 UNION ALL
SELECT N '20路 ' ,N '站J ',4 UNION ALL
SELECT N '20路 ' ,N '站L ',5 UNION ALL
SELECT N '20路 ' ,N '站M ',6 UNION ALL
SELECT N '255路 ',N '站N ',1 UNION ALL
SELECT N '255路 ',N '站O ',2 UNION ALL
SELECT N '255路 ',N '站P ',3 UNION ALL
SELECT N '255路 ',N '站Q ',4 UNION ALL
SELECT N '255路 ',N '站J ',5 UNION ALL
SELECT N '255路 ',N '站D ',6 UNION ALL
SELECT N '255路 ',N '站E ',7 UNION ALL
SELECT N '255路 ',N '站F ',8
GO

--乘车线路查询存储过程
CREATE PROC p_qry
@Station_Start nvarchar(10),
@Station_Stop nvarchar(10)
AS
SET NOCOUNT ON
DECLARE @l int
SET @l=0
SELECT ID,Station,
Line=CAST( '( '+RTRIM(ID)+ ': '+RTRIM(Station) as nvarchar(4000)),
Orders=Orders,
[Level]=@l
INTO # FROM T_Line
WHERE Station=@Station_Start
WHILE @@ROWCOUNT> 0
AND NOT EXISTS(SELECT * FROM # WHERE Station=@Station_Stop)
BEGIN
SET @l=@l+1
INSERT #(Line,ID,Station,Orders,[Level])
SELECT
Line=a.Line+CASE
WHEN a.ID=b.ID THEN N '-> '+RTRIM(b.Station)
ELSE N ') ∝ ( '+RTRIM(b.ID)
+N ': '+RTRIM(b.Station) END,
b.ID,b.Station,b.Orders,@l
FROM # a,T_Line b
WHERE a.[Level]=@l-1
AND(a.Station=b.Station AND a.ID <> b.ID
OR a.ID=b.ID AND(
a.Orders=b.Orders+1
OR
a.Orders=b.Orders-1))
AND LEN(a.Line) <4000
AND PATINDEX( '%[ > ] '+b.Station+ '[-)]% ',a.Line)=0
END
SELECT N '起点站 '=@Station_Start
,N '终点站 '=@Station_Stop
,N '乘车线路 '=Line+N ') '
FROM #
WHERE [Level]=@l
AND Station=@Station_Stop
IF @@ROWCOUNT =0 --如果未有可以到达的线路,则显示处理结果表备查
SELECT * FROM #
GO

--调用
EXEC p_qry N '站A ',N '站L '
/*--结果
起点站 终点站 乘车线路
---------- ------------ -----------------------
站A 站L (8路: 站A-> 站B-> 站C-> 站D-> 站J-> 站L)
--*/

热点排行