Hvis du garanterer, at dataene i din kildetabel vil være i denne særlige rækkefølge, kan du skrive en lignende forespørgsel for at opnå det ønskede resultat:
-- sample of data from your question
SQL> with t1(col) as(
2 select 'Scott' from dual union all
3 select '100' from dual union all
4 select '10' from dual union all
5 select 'Miller' from dual union all
6 select '200' from dual union all
7 select '20' from dual union all
8 select 'Mike' from dual union all
9 select '300' from dual union all
10 select '30' from dual union all
11 select 'Allen' from dual union all
12 select '400' from dual union all
13 select '40' from dual
14 ) -- the query
15 select max(decode(mod(rownum - 1, 3), 0, col)) as name
16 , max(decode(mod(rownum - 1, 3), 1, col)) as sal
17 , max(decode(mod(rownum - 1, 3), 2, col)) as depno
18 from t1
19 group by trunc((rownum -1)/ 3)
20 /
NAME SAL DEPNO
------ ------ ------
Miller 200 20
Mike 300 30
Allen 400 40
Scott 100 10
Tillæg
Du behøver ikke en markør til dette (medmindre det er en slags specielle krav, der kræver brug af markører). For at udfylde en anden tabel med data kan du simpelthen bruge INSERT INTO ... SELECT
:
insert into temp_process(name, sal, depno)
select max(decode(mod(rownum - 1, 3), 0, col))
, max(decode(mod(rownum - 1, 3), 1, col))
, max(decode(mod(rownum - 1, 3), 2, col))
from table_a
group by trunc((rownum -1)/ 3)