alexbancheva

Task 7_LongestAndShortestTrips_21June2020

Feb 4th, 2021
1,374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.47 KB | None | 0 0
  1. SELECT a.Id AS AccountId, CONCAT(a.FirstName, ' ', a.LastName) AS FullName, MAX(DATEDIFF(DAY, t.ArrivalDate, t.ReturnDate)) AS LongestTrip,
  2.     MIN(DATEDIFF(DAY, t.ArrivalDate, t.ReturnDate)) AS ShortestTrip
  3.         FROM Trips AS t
  4.         JOIN AccountsTrips AS [at] ON t.Id = [at].TripId
  5.         LEFT JOIN Accounts AS a ON [at].AccountId = a.Id
  6.         WHERE a.MiddleName IS NULL AND t.CancelDate IS NULL
  7.         GROUP BY a.Id, a.FirstName, a.LastName
  8.         ORDER BY LongestTrip DESC,
  9.                     ShortestTrip ASC
Advertisement
Add Comment
Please, Sign In to add comment