Come posso impostare il ciclo nella funzione di query PL / SQL che il risultato di ciascun loop è stato inserito in diverse colonne e tutto in una tabella

StackOverflow https://stackoverflow.com//questions/11706946

  •  13-12-2019
  •  | 
  •  

Domanda

Come posso impostare il ciclo nella funzione di query PL / SQL che il risultato di ciascun loop Out inserisce in colonne diverse e tutte in una tabella.

La funzione è:

select t1.aaa, coalesce(t2.bbb_count, 0) bbb_count,
    coalesce(t2.ccc_sum, 0) ccc_sum
from (
  select distinct aaa
  from nrb
) t1
left join (
  select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
  from nrb t
  where t.vvv IN  ('3','4','5','6','D','E','F')
    and t.ddd like '50%'
    and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
    and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
  group by t.aaa
) t2 on t1.aaa = t2.aaa
order by t1.aaa;
.

Questa funzione mi darà un tavolo con 200 righe e 3 colonne.Ho bisogno di fare un anello in

"e t.ddd come '50% '"

Linea, da 50 a 55. Intendo che il risultato sarà 200 righe e 15 colonne.

PL / SQL 7.0.2 Utente illimitato LISENCE

OCI: 9.2

Oracle DB: 11.1.0.6.0 Enterprise

OS: Win XP

È stato utile?

Soluzione

select t1.aaa, 
       coalesce(t2.bbb_count, 0) bbb_50_count, coalesce(t2.ccc_sum, 0) ccc_50_sum,
       coalesce(t3.bbb_count, 0) bbb_51_count, coalesce(t3.ccc_sum, 0) ccc_51_sum,
       coalesce(t4.bbb_count, 0) bbb_52_count, coalesce(t4.ccc_sum, 0) ccc_52_sum,
       coalesce(t5.bbb_count, 0) bbb_53_count, coalesce(t5.ccc_sum, 0) ccc_53_sum,
       coalesce(t6.bbb_count, 0) bbb_54_count, coalesce(t6.ccc_sum, 0) ccc_54_sum,
       coalesce(t7.bbb_count, 0) bbb_55_count, coalesce(t7.ccc_sum, 0) ccc_55_sum
from (
  select distinct aaa
  from nrb
) t1
left join (
  select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
  from nrb t
  where t.vvv IN  ('3','4','5','6','D','E','F')
    and t.ddd like '50%'
    and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
    and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
  group by t.aaa
) t2 on t1.aaa = t2.aaa
left join (
  select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
  from nrb t
  where t.vvv IN  ('3','4','5','6','D','E','F')
    and t.ddd like '51%'
    and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
    and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
  group by t.aaa
) t3 on t1.aaa = t3.aaa
left join (
  select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
  from nrb t
  where t.vvv IN  ('3','4','5','6','D','E','F')
    and t.ddd like '52%'
    and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
    and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
  group by t.aaa
) t4 on t1.aaa = t4.aaa
left join (
  select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
  from nrb t
  where t.vvv IN  ('3','4','5','6','D','E','F')
    and t.ddd like '53%'
    and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
    and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
  group by t.aaa
) t5 on t1.aaa = t5.aaa
left join (
  select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
  from nrb t
  where t.vvv IN  ('3','4','5','6','D','E','F')
    and t.ddd like '54%'
    and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
    and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
  group by t.aaa
) t6 on t1.aaa = t6.aaa
left join (
  select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
  from nrb t
  where t.vvv IN  ('3','4','5','6','D','E','F')
    and t.ddd like '55%'
    and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
    and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
  group by t.aaa
) t7 on t1.aaa = t7.aaa
order by t1.aaa;
.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top