简体   繁体   English

带递归Postgres查询的约束

[英]Constraints w/ Recursive Postgres Query

I'm looking to skip a certain city as I traverse my data. 在遍历数据时,我希望跳过某个城市。 Currently, this query works to find all available flights from SLC to LA, including trips with layovers. 当前,此查询可查找从SLC到LA的所有可用航班,包括中途停留的航班。 You'll see this in the picture below. 您将在下面的图片中看到它。

从SLC飞往洛杉矶的航班

However, I want to be able to exclude certain cities in a flight plan. 但是,我希望能够在飞行计划中排除某些城市。 For example, if Montreal is a stop between SLC and LA, that trip wouldn't be considered. 例如,如果蒙特利尔是SLC和LA之间的停靠站,则不会考虑该行程。 I've tried putting various things in the WHERE clauses, but to no avail. 我尝试将各种内容放在WHERE子句中,但无济于事。 Any other suggestions? 还有其他建议吗? Sample data an queries are given below. 下面给出查询的示例数据。

WITH RECURSIVE segs AS (
  SELECT f0.flight_num::text as flight
            , src_city, dest_city
            , dep_time AS departure
            , arr_time AS arrival
            , airfare, mileage
            , 1 as hops
            , (arr_time - dep_time)::interval AS total_time
            , '00:00'::interval as waiting_time
  FROM flight f0
  WHERE src_city = 'SLC' -- <SRC_CITY>
  UNION ALL
  SELECT s.flight || '-->' || f1.flight_num::text as flight
            , s.src_city, f1.dest_city
            , s.departure AS departure
            , f1.arr_time AS arrival
            , s.airfare + f1.airfare as airfare
            , s.mileage + f1.mileage as mileage
            , s.hops + 1 AS hops
            , s.total_time + (f1.arr_time - f1.dep_time)::interval AS total_time
            , s.waiting_time + (f1.dep_time - s.arrival)::interval AS waiting_time
  FROM segs s
     JOIN flight f1
       ON f1.src_city = s.dest_city
       AND f1.dep_time > s.arrival -- you can't leave until you are there
)
SELECT *
FROM segs
WHERE dest_city = 'LA' -- <DEST_CITY>
ORDER BY airfare desc
    ;

create table flight
  ( flight_num BIGSERIAL PRIMARY KEY
  , src_city varchar
  , dest_city varchar
  , dep_time TIME
  , arr_time TIME
  , airfare INTEGER
  , mileage INTEGER
);

insert into flight VALUES
  (101,    'Montreal',          'NY',                   '05:30',     '06:45',    180,      170),
  (102,    'Montreal',          'Washington',           '01:00',     '02:35',    100,      180),
  (103,    'NY',                'Chicago',              '08:00',     '10:00',    150,      300),
  (105,    'Washington',        'KansasCity',           '06:00',     '08:45',    200,      600),
  (106,    'Washington',        'NY',                   '12:00',     '13:30',     50,       80),
  (107,    'Chicago',           'SLC',                  '11:00',     '14:30',    220,      750),
  (110,    'KansasCity',        'Denver',               '14:00',     '15:25',    180,      300),
  (111,    'KansasCity',        'SLC',                  '13:00',     '15:30',    200,      500),
  (112,    'SLC',               'SanFran',              '18:00',     '19:30',     85,      210),
  (113,    'SLC',               'LA',                   '17:30',     '19:00',    185,      230),
  (115,    'Denver',            'SLC',                  '15:00',     '16:00',     75,      300),
  (116,    'SanFran',           'LA',                   '22:00',     '22:30',     50,       75),
  (118,    'LA',                'Seattle',              '20:00',     '21:00',    150,      450);

To exclude certain cities from the flight plan you should add where clauses at 2 places in your query as following: 要从飞行计划中排除某些城市,您应该在查询的2个地方添加where子句,如下所示:

  1. Right after src_city condition 在src_city条件之后

     ... WHERE src_city = 'SLC' -- <SRC_CITY> AND dest_city <> 'Montreal' ... 
  2. In the recursive join condition 在递归联接条件下

     ... AND f1.dep_time > s.arrival -- you can't leave until you are there AND f1.dest_city <> 'Montreal' ... 

    I don't have Postgress but I tried it with SQL server and it seems to work. 我没有Postgress,但是我在SQL Server上尝试了它,它似乎可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM