1

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..

4

1 回答 1

0

没有描述为什么 AP4/ST4 是一种特殊情况,但假设您尝试在 ST 中为每个 (sp,ap) 组合 3 个连续元素:

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  

PS 对于替代结果输出:

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  
于 2014-04-08T14:47:07.990 回答