Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, mrunmayi.

Asked: April 27, 2016 - 11:38 am UTC

Last updated: April 29, 2016 - 4:42 pm UTC

Version: Oracle 11g

Viewed 50K+ times! This question is

You Asked

hi, tom

my table is as follow

acct_no code meaning

123 ABC abc
123 BVC bvc
123 DDD ddd
123 ERT ert
123 ETY ety
123 KJH kjh
123 FE fe
123 TTT ttt
123 WCC wcc
123 IUY iuy

my expected out is as follow :

Acct_no code_1 meaning_1 code_2 meaning_2 code_3 meaning_3 code_4 meaning_4 code_5 meaning_5 code_6 meaning_6 code_7 meaning_7 code_8 meaning_8 code_9 meaning_9

123 ABC abc BVC bvc DDD ddd ERT ert ETY ety KJH kjh FE fe TTT ttt WCC wcc

I am using Oracle 11g and i need to Use PIVOT. it would be great if you help me fror this.

thanks

and Chris said...

Include each column you want to turn the rows into columns to your pivot clause. For example:

create table t (
  acct_no varchar2(3), code varchar2(3), meaning varchar2(3)
);

insert into t values ('123','ABC','abc');
insert into t values ('123','BVC','bvc');
insert into t values ('123','DDD','ddd');

select * from t
pivot (
   max(code) as cd, max(meaning) as mn 
   for  (code) in (
     'ABC' as a, 'BVC' as b, 'DDD' as d
   )
);

ACC A_C A_M B_C B_M D_C D_M
--- --- --- --- --- --- ---
123 ABC abc BVC bvc DDD ddd


For each additional row you want to convert into a column, add the values to the for (code) in () clause as needed.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Need further Help

mrunmayi kusurkar, April 27, 2016 - 1:50 pm UTC

create table t (
acct_no varchar2(3), code varchar2(3), meaning varchar2(3), seq_no number
); we can have multiple acct_no is given table. for given acct_no = 123 code is stored in specific sequence which is there in seq_no. according to that seq no i need to display in one row only. eg: "ABC" has seq_no= 1 then it should be 1 st displayed. if "KJH" has seq_no = 2 it should be displayed next after "ABC". if you any idea how we can do this
Chris Saxon
April 29, 2016 - 10:33 am UTC

You mean something like this:

create table t (
  acct_no varchar2(3), code varchar2(3), meaning varchar2(3)
);

insert into t values ('123','ABC','abc');
insert into t values ('123','BVC','bvc');
insert into t values ('123','DDD','ddd');

insert into t values ('456','JKL','abc');
insert into t values ('456','MNO','bvc');

select * from (
  select t.*, 
         row_number() over (partition by acct_no order by code) rn 
  from t
)
pivot (
  max(code) as cd, max(meaning) as mn
  for (rn) in (1 as one, 2 as two, 3 as three)
);

ACC ONE ONE TWO TWO THR THR
--- --- --- --- --- --- ---
123 ABC abc BVC bvc DDD ddd
456 JKL abc MNO bvc


?

Reply for the 2nd followup

Sai Mettukuri, April 29, 2016 - 12:07 pm UTC

Chris- In the latest follow up from the OP he added a seq_no column to the table and it seems he wants to sort on that accordingly. So i'm thinking we just need to put 'order by' at the end of your query.


create table t (
acct_no varchar2(3), code varchar2(3), meaning varchar2(3),seq_no number
);

insert into t values ('123','ABC','abc',4);
insert into t values ('123','BVC','bvc',4);
insert into t values ('123','DDD','ddd',4);

insert into t values ('456','JKL','abc',2);
insert into t values ('456','MNO','bvc',2);

select * from (
select t.*,
row_number() over (partition by acct_no order by code) rn
from t
)
pivot (
max(code) as cd, max(meaning) as mn
for (rn) in (1 as one, 2 as two, 3 as three)
)
order by seq_no;

ACCT_NO SEQ_NO ONE ONE TWO TWO THR THR
456 2 JKL abc MNO bvc - -
123 4 ABC abc BVC bvc DDD ddd


Chris Saxon
April 29, 2016 - 4:42 pm UTC

Good catch, I missed that.

I read it as this shows which order the rows become columns. If this is the case, the analytic:

row_number() over (partition by acct_no order by code) rn 


should change to use it in the order by:

row_number() over (partition by acct_no order by seq_no) rn