Home > OS >  Using the results of one SQL query in another query (Athena)
Using the results of one SQL query in another query (Athena)

Time:08-18

I'm a bit stuck on how can do this so hoping someone can point me in the right direction.

I have this simple query:

SELECT acchl.is_current,
       acchl.aircraft_registration_number,
       acchl.aircraft_transponder_code
FROM   fleets.aircraft_all_history_latest acchl
WHERE  acchl.is_current = true

Which I then want to use the results from this query to find all the duplicate aircraft transponder codes along with the aircraft registration numbers with something like a self join to fetch the actual rows that may have duplicate values using something like this:

select s.id, s.col_maybe_dups 
  from sometab s 
  join (select col_maybe_dups as cd 
          from sometab 
         group by cd 
        having count(*) > 1) x 
    on x.cd = s.col_maybe_dups; 

CodePudding user response:

Looks good!

You could WITH it too...

WITH data as (
SELECT acchl.is_current,
       acchl.aircraft_registration_number,
       acchl.aircraft_transponder_code
FROM   fleets.aircraft_all_history_latest acchl
WHERE  acchl.is_current
)

select * from data
where aircraft_transponder_code in 
  (select aircraft_transponder_code 
   from data 
   group by 1
   having count(*) > 1
  )
  • Related