title: Need a SQL Server query to eliminate the Highlighted Rows ( Returning Routes in Flight)
body: <p>I have a requirement where I need to eliminate all the rows which have returning flight routes.</p>
  <p>I have highlighted the routes in the orange color which depicts this behavior.</p>
  <p>For example, row 1 has one route <code>BKI - MYY</code> and row 4 has <code>MYY - BKI</code>. I need a flag (Boolean 1/0) that checks the entire row and sees if it's a returning flight or not.</p>
  <p>Note: there are a few scenarios where the flight started at 10:00 PM night today and returned the next day early morning. if it is a consecutive day then we need to eliminate that rows as well.</p>
  <p>Any thought on how to solve this?</p>
  <p><strong>Gsheet link</strong> : Sample Data : <a href="https://docs.google.com/spreadsheets/d/1ibrUfOTMEx4i-erDQnIfszdcEvJKU_oFcnMBFcGbvyM/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/1ibrUfOTMEx4i-erDQnIfszdcEvJKU_oFcnMBFcGbvyM/edit?usp=sharing</a></p>
  <p><strong>Definition:</strong> Return flight is a two-way journey by air. If a flight is starting from the source and after certain hop/route returns backs to the same source (Departure Station)</p>
  <p><strong>std_utc</strong> = Departure Time
  <strong>Sta_utc</strong> = Arival Time</p>
  <p><a href="https://i.stack.imgur.com/bu9Lg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bu9Lg.png" alt="enter image description here" /></a></p>
  <pre><code>with AllFlight as
  (
  select row_number()over(order by std_utc)rn, aoc,hub,flight ,departure,arrival,std_utc,sta_utc
  from TABLENAME01
  ),
  returnFlight as 
  (
  select * from AllFlight s
  where exists 
  (
      select * from AllFlight s2 
      where s2.rn&lt;s.rn and s2.arrival=s.departure and s2.departure=s.arrival 
      and 
      (date_diff(EXTRACT(DATE FROM s.sta_utc),EXTRACT(DATE FROM s2.std_utc), DAY))&lt;=1
  )
  )
  
  select aoc,hub,flight ,departure ,arrival,std_utc  ,sta_utc from AllFlight where rn not in (select rn from returnFlight)
  
  </code></pre>
  <p>This query is kept on running and not giving me the desired output.
  Note: I am using Bigquery</p>