Advertisement
Guest User

Untitled

a guest
Jun 5th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.89 KB | None | 0 0
  1. SELECT
  2.     top 1
  3.     CONCAT(f0.from_city, COALESCE('-' + f0.to_city, ''), COALESCE('-' + f1.to_city, ''), COALESCE('-' + f2.to_city, ''),
  4.            COALESCE('-' + f3.to_city, '')) AS route
  5.   ,
  6.     (f0.duration + ISNULL(f1.duration, 0) + ISNULL(f2.duration, 0) + ISNULL(f3.duration, 0)) AS total_duration
  7.     FROM
  8.         flights f0
  9.             LEFT JOIN flights f1
  10.                       ON f1.from_city = f0.to_city AND f1.to_city NOT IN (f0.from_city)
  11.             LEFT JOIN flights f2
  12.                       ON f2.from_city = f1.to_city AND f2.to_city NOT IN (f0.from_city, f1.from_city)
  13.             LEFT JOIN flights f3
  14.                       ON f3.from_city = f2.to_city AND f3.to_city NOT IN (f0.from_city, f1.from_city, f2.from_city)
  15.     WHERE
  16.           f0.from_city = 'Kharkiv'
  17.       AND 'Vancouver' IN (f0.to_city, f1.to_city, f2.to_city, f3.to_city)
  18.     ORDER BY total_duration, route
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement