我希望使用asp.net web api& amp; sql server数据库。
以下是时间表的示例数据库设计。我使用此查询来尝试查找时间表的算法。
我目前的问题是,当我尝试来源和目标[A-B]
时,它也会重新运行[B-A]
个结果。我仍然没有考虑时间因素。您的宝贵建议也表示赞赏。
预期产出:
我还有暂停的地理位置..
DECLARE @Souce INT = 2 , @Destination INT = 11
DECLARE @Routes TABLE(
ID INT,
RouteStart VARCHAR(MAX),
RouteStop VARCHAR(MAX)
)
DECLARE @TrainTimeTable TABLE(
ID INT,
RoutID INT,
Sequance INT, -- determine the next station of the same route
BeforeStation INT,
CurrentStation INT,
[Arr.Time] TIME,
[Dep.Time] TIME
)
DECLARE @Halts TABLE(
ID INT,
Name VARCHAR(MAX)
)
INSERT INTO @Routes VALUES (1,'A','B'),
(2,'B', 'C'),
(3,'B', 'A');
INSERT INTO @Halts VALUES (1,'A1'),
(2,'A2'),
(3,'A3'),
(4,'A4'),
(5,'A5'),
(6,'A6'),
(7,'B1'),
(8,'B2'),
(9,'B3'),
(10,'B4'),
(11,'B5'),
(12,'B6');
/*A To B*/
INSERT @TrainTimeTable VALUES (1,1,1,1,1,'06:00','6:01'),
(2,1,2,1,2,'6:05' ,'6:06'),
(3,1,3,2,3,'6:10' ,'6:11'),
(4,1,4,3,4,'6:15' ,'6:16'),
(5,1,5,4,5,'6:20' ,'6:21'),
(6,1,6,5,6,'6:25' ,'6:25'); -- Stops here
/*B To C*/
INSERT @TrainTimeTable VALUES (7,2,1,6,6,'6:30','6:31'),
(8,2,2,6,7,'6:35','6:36'),
(9,2,3,7,8,'6:40','6:41'),
(10,2,4,8,9,'6:45','6:46'),
(11,2,5,9,10,'6:50','6:51'),
(12,2,6,10,11,'7:00','7:01'),
(13,2,7,11,12,'7:10','7:10'); -- stops here
/*B to A*/
INSERT @TrainTimeTable VALUES (14,3,1,6,6,'06:00','6:01'),
(15,3,2,6,5,'6:05' ,'6:06'),
(16,3,3,5,4,'6:10' ,'6:11'),
(17,3,4,4,3,'6:15' ,'6:16'),
(18,3,5,3,2,'6:20' ,'6:21'),
(19,3,6,2,1,'6:25' ,'6:25'); -- Stops here
SELECT tc.*,
hl1.Name AS 'BeforeName',
hl2.Name AS 'CurrentName',
rr.RouteStart + ' - '+rr.RouteStop AS 'Route'
FROM @TrainTimeTable tc
INNER JOIN @Routes rr ON rr.ID = tc.RoutID
INNER JOIN @Halts hl1 ON hl1.ID = tc.BeforeStation
INNER JOIN @Halts hl2 ON hl2.ID = tc.CurrentStation
WHERE tc.CurrentStation BETWEEN @Souce AND @Destination
答案 0 :(得分:0)
你走了:
SELECT tc.*,
hl1.Name AS 'BeforeName',
hl2.Name AS 'CurrentName',
rr.RouteStart + ' - '+rr.RouteStop AS 'Route'
FROM @TrainTimeTable tc
INNER JOIN @Routes rr ON rr.ID = tc.RoutID
LEFT JOIN @Halts hl1 ON hl1.ID = tc.BeforeStation
LEFT JOIN @Halts hl2 ON hl2.ID = tc.CurrentStation
WHERE tc.CurrentStation BETWEEN @Souce AND @Destination
AND tc.RoutID < 3;
结果:
+----+--------+----------+---------------+----------------+----------+----------+------------+-------------+-------+
| ID | RoutID | Sequance | BeforeStation | CurrentStation | Arr.Time | Dep.Time | BeforeName | CurrentName | Route |
+----+--------+----------+---------------+----------------+----------+----------+------------+-------------+-------+
| 2 | 1 | 2 | 1 | 2 | 06:05:00 | 06:06:00 | A1 | A2 | A - B |
| 3 | 1 | 3 | 2 | 3 | 06:10:00 | 06:11:00 | A2 | A3 | A - B |
| 4 | 1 | 4 | 3 | 4 | 06:15:00 | 06:16:00 | A3 | A4 | A - B |
| 5 | 1 | 5 | 4 | 5 | 06:20:00 | 06:21:00 | A4 | A5 | A - B |
| 6 | 1 | 6 | 5 | 6 | 06:25:00 | 06:25:00 | A5 | A6 | A - B |
| 7 | 2 | 1 | 6 | 6 | 06:30:00 | 06:31:00 | A6 | A6 | B - C |
| 8 | 2 | 2 | 6 | 7 | 06:35:00 | 06:36:00 | A6 | B1 | B - C |
| 9 | 2 | 3 | 7 | 8 | 06:40:00 | 06:41:00 | B1 | B2 | B - C |
| 10 | 2 | 4 | 8 | 9 | 06:45:00 | 06:46:00 | B2 | B3 | B - C |
| 11 | 2 | 5 | 9 | 10 | 06:50:00 | 06:51:00 | B3 | B4 | B - C |
| 12 | 2 | 6 | 10 | 11 | 07:00:00 | 07:01:00 | B4 | B5 | B - C |
+----+--------+----------+---------------+----------------+----------+----------+------------+-------------+-------+
答案 1 :(得分:0)
您可以将此谓词添加到where子句的结尾。
AND tc.ID <= (SELECT TOP 1 ID FROM @TrainTimeTable WHERE CurrentStation = @Destination ORDER BY ID)