递归查询
我想得到关于我想做的递归查询的帮助。 我在 DB2 中创建了这个表:
connect reset; connect to sample; DROP TABLE FLIGHTS; CREATE TABLE FLIGHTS (START VARCHAR(16) NOT NULL, DESTINATION VARCHAR(16) NOT NULL, DISTANCE BIGINT NOT NULL ); insert into FLIGHTS values ('Dublin','Bhogrol',5340); insert into FLIGHTS values ('Dublin','Smallville',5500); insert into FLIGHTS values ('Smallville','Seattle',1300); insert into FLIGHTS values ('Smallville','Clacton',6700); insert into FLIGHTS values ('Bhogrol','Moscow',2320); insert into FLIGHTS values ('Moscow','Seattle',3600); insert into FLIGHTS values ('Bhogrol','Smallville',2950); insert into FLIGHTS values ('Rome','Bhogrol',720); insert into FLIGHTS values ('Clacton','Moscow',6700); insert into FLIGHTS values ('Rome','Smallville',3050); insert into FLIGHTS values ('Schippol','Smallville',8990); insert into FLIGHTS values ('Seattle','Schippol',7840); insert into FLIGHTS values ('Bhogrol','Clacton',1300); insert into FLIGHTS values ('Bilbao','Moscow',1270); insert into FLIGHTS values ('Smallville','Schippol',8990);
我想查找从都柏林出发且少于 8 次的所有转机航班 停止。查询的输出应该是具有以下 3 个字段的一组行 格式:[路线,距离,停靠点]其中路线字段是由 从都柏林出发的每条连接路径所访问的机场。这是机场名称的序列 用“-”字符分隔。但不要显示整个机场名称,只需显示第一个 三个字符,使每个字符串看起来像 例如,Dub>Sch>Dub>Sma>Sch>Dub>Sma>Cla>Mos。 属性“route”的类型应为 VARCHAR(40)。
我想这样开始:
WITH path (start, destination, distance, stops) AS( SELECT f.start, f.destination, f.distance, 0 FROM flights f WHERE start = 'Dublin' UNION ALL SELECT p.start, f.destination, p.distance + f.distance, p.stops+1 FROM flights f, path p WHERE p.destination = f.start AND p.stops < 8 ) SELECT start, destination, distance, stops FROM path;
这是正确的吗?如果是,那我该怎么办?
i would like your help about a recursive query which I want to do.
I created this table in DB2:
connect reset; connect to sample; DROP TABLE FLIGHTS; CREATE TABLE FLIGHTS (START VARCHAR(16) NOT NULL, DESTINATION VARCHAR(16) NOT NULL, DISTANCE BIGINT NOT NULL ); insert into FLIGHTS values ('Dublin','Bhogrol',5340); insert into FLIGHTS values ('Dublin','Smallville',5500); insert into FLIGHTS values ('Smallville','Seattle',1300); insert into FLIGHTS values ('Smallville','Clacton',6700); insert into FLIGHTS values ('Bhogrol','Moscow',2320); insert into FLIGHTS values ('Moscow','Seattle',3600); insert into FLIGHTS values ('Bhogrol','Smallville',2950); insert into FLIGHTS values ('Rome','Bhogrol',720); insert into FLIGHTS values ('Clacton','Moscow',6700); insert into FLIGHTS values ('Rome','Smallville',3050); insert into FLIGHTS values ('Schippol','Smallville',8990); insert into FLIGHTS values ('Seattle','Schippol',7840); insert into FLIGHTS values ('Bhogrol','Clacton',1300); insert into FLIGHTS values ('Bilbao','Moscow',1270); insert into FLIGHTS values ('Smallville','Schippol',8990);
I want to find all connected flights from Dublin with less than eight
stops. The output from the query should be a set of rows with the following 3-field
format: [ route, distance, stops ] where the route field is a string built up from the
airports visited in each connected path from Dublin. It is a sequence of airport names
separated by the '-' character. But instead of the whole airport name, just show the first
three characters, so that each string looks like
Dub>Sch>Dub>Sma>Sch>Dub>Sma>Cla>Mos, for example.
The type of attribute 'route' should be VARCHAR(40).
I thought to start in this way:
WITH path (start, destination, distance, stops) AS( SELECT f.start, f.destination, f.distance, 0 FROM flights f WHERE start = 'Dublin' UNION ALL SELECT p.start, f.destination, p.distance + f.distance, p.stops+1 FROM flights f, path p WHERE p.destination = f.start AND p.stops < 8 ) SELECT start, destination, distance, stops FROM path;
Is it correct?If yes, what can i do then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你正在寻找这样的东西:
I think you are looking for something like this: