Question

I need to do what I thought was going to be a simple SQL query.. but I got stuck deciding how can these be grouped:

    <p>I've got the below table:</p>
Company    | Airport    |     Type
------------------------------------------
SP1        | AP1        |     ST1             
SP1        | AP1        |     ST2        
SP1        | AP1        |     ST3        
SP1        | AP2        |     ST1         
SP1        | AP2        |     ST2        
SP1        | AP2        |     ST3 
SP1        | AP3        |     ST1 
SP1        | AP3        |     ST2 
SP1        | AP4        |     ST1 
SP1        | AP4        |     ST2 
SP1        | AP4        |     ST3 
SP1        | AP4        |     ST4 

I want to group AP and ST in the following way so that the desired result is like this:

(CASE 1)

SP         | AP             |     ST
------------------------------------------
SP1        | AP1, AP2, AP4  |     ST1, ST2, ST3
SP1        | AP3            |     ST1, ST2        
SP1        | AP4            |     ST4    

Any thoughts? Really appreciated!

Update

As pointed out, there is another alternative for the result:

(CASE 2)

SP         | AP             |     ST
------------------------------------------
SP1        | AP1, AP2       |     ST1, ST2, ST3
SP1        | AP3            |     ST1, ST2        
SP1        | AP4            |     ST1, ST2, ST3, ST4    

I've also added titles to the columns to give a bit more context. The idea is just that, to be able to group associated elements. I'm happy with any of the two results, hopefully both alternatives if possible..

Was it helpful?

Solution

It's not described why AP4/ST4 is a special case, but supposing you try to group withing 3 sequential elements in ST for each (sp,ap):

SQL> with t (SP, AP, ST) as (
  2  select 'SP1','AP1','ST1' from dual union all
  3  select 'SP1','AP1','ST2' from dual union all
  4  select 'SP1','AP1','ST3' from dual union all
  5  select 'SP1','AP2','ST1' from dual union all
  6  select 'SP1','AP2','ST2' from dual union all
  7  select 'SP1','AP2','ST3' from dual union all
  8  select 'SP1','AP3','ST1' from dual union all
  9  select 'SP1','AP3','ST2' from dual union all
 10  select 'SP1','AP4','ST1' from dual union all
 11  select 'SP1','AP4','ST2' from dual union all
 12  select 'SP1','AP4','ST3' from dual union all
 13  select 'SP1','AP4','ST4' from dual
 14  )
 15  select sp, listagg(ap,',') within group (order by ap) lstap, lstst
 16  from (
 17  select sp, ap, listagg(st,',') within group (order by st) lstst from (
 18  select sp, ap, st, ceil((row_number() over(partition by sp, ap order by st))/3) grp
 19  from t
 20  )
 21  group by sp, ap, grp
 22  )
 23  group by sp, lstst
 24  order by 1,2,3
 25  /

SP  LSTAP                     LSTST                                             
--- ------------------------- -------------------------                         
SP1 AP1,AP2,AP4               ST1,ST2,ST3                                       
SP1 AP3                       ST1,ST2                                           
SP1 AP4                       ST4  

P.S. For alternative result output:

SQL> with t (SP, AP, ST) as (
  2  select 'SP1','AP1','ST1' from dual union all
  3  select 'SP1','AP1','ST2' from dual union all
  4  select 'SP1','AP1','ST3' from dual union all
  5  select 'SP1','AP2','ST1' from dual union all
  6  select 'SP1','AP2','ST2' from dual union all
  7  select 'SP1','AP2','ST3' from dual union all
  8  select 'SP1','AP3','ST1' from dual union all
  9  select 'SP1','AP3','ST2' from dual union all
 10  select 'SP1','AP4','ST1' from dual union all
 11  select 'SP1','AP4','ST2' from dual union all
 12  select 'SP1','AP4','ST3' from dual union all
 13  select 'SP1','AP4','ST4' from dual
 14  )
 15  select sp, listagg(ap,',') within group (order by ap) lstap, lstst
 16  from (
 17  select sp, ap, listagg(st,',') within group (order by st) lstst from (
 18  select sp, ap, st
 19  from t
 20  )
 21  group by sp, ap
 22  )
 23  group by sp, lstst
 24  order by 1,2,3
 25  /

SP  LSTAP                     LSTST                                             
--- ------------------------- -------------------------                         
SP1 AP1,AP2                   ST1,ST2,ST3                                       
SP1 AP3                       ST1,ST2                                           
SP1 AP4                       ST1,ST2,ST3,ST4  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top