Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a question in Pivoting data in Oracle . Here is my data looks like

ID, TaskName, Type, Date
44400 M0 A 1/1/2015
44400 M1 A 1/3/2015
44400 M2 A 1/4/2015
44400 M1 CF 2/1/2105
44400 DG1 CF 2/2/2015
44400 M0 POR 2/11/2015
45000 M0 A 2/1/2015
45000 M1 A 2/3/2015
45000 M2 A 2/4/2015
45000 M1 CF 3/1/2105
45000 DG1 CF 3/2/2015
45000 M0 POR 3/11/2015

and I want to pivot above data and need in below form dynamically.

Now, I want data as below

ID M0_A M1_A M2_A M1_CF DG1_CF M0_POR
44400 1/1/2015 1/3/2015 1/4/2015 2/1/2015 2/2/2015 2/11/2015
45000 2/1/2015 2/3/2015 2/4/2015 3/1/2015 3/2/2015 3/11/2015

I really appreciate your help. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
134 views
Welcome To Ask or Share your Answers For Others

1 Answer

For defined number of pairs of values in columns tname, ttype you can use below query (note that I changed your column names from example, because you used Oracle keywords there, also I named table as tasks, so you will have to change this data to your real column names and table name everywhere in code) :

select * from tasks 
pivot (max(tdate) for (tname, ttype) in 
  (('DG1','CF') DG1_CF, ('M0','A')  M0_A,  ('M0','POR') M0_POR,
   ('M1','A'  ) M1_A,   ('M1','CF') M1_CF, ('M2','A')   M2_A)));

For dynamic number of possibilities you will need some procedure "creating" this query. Here I used view for this. Copy procedure code and compile it. When data in your table changes you have to run procedure at first, then simply select from view created by procedure. In order to run properly your schema needs privilleges for creating views granted.

execute create_tasks_view;
select * from v_tasks;

anonymous block completed
   ID DG1_CF     M0_A       M0_POR     M1_A       M1_CF      M2_A     
----- ---------- ---------- ---------- ---------- ---------- ----------
45000 2015-03-02 2015-02-01 2015-03-11 2015-02-03 2015-03-01 2015-02-04 
44400 2015-02-02 2015-01-01 2015-02-11 2015-01-03 2015-02-01 2015-01-04

Of course you can change ordering of rows and columns as you wish by adding or modifying order by parts in procedure code:

create or replace procedure create_tasks_view as 
  v_sql varchar2(32767) := '';
begin
  for v in (select distinct tname, ttype from tasks order by tname, ttype) 
  loop
    v_sql := v_sql || '(''' || v.tname || ''',''' || v.ttype || ''') '
      ||v.tname||'_'||v.ttype||',';
  end loop;
  v_sql := 'create or replace view v_tasks as '
    ||'select * from tasks pivot (max(tdate) for (tname, ttype) in ('
    ||rtrim(v_sql, ', ')||'))'; 
  execute immediate v_sql;
end create_tasks_view;

I believe there is also more universal solution for your question in link I gave you in comments: Dynamic SQL Pivoting.... It looks very promising, just read carefully section Resources at bottom, and follow the steps of instruction. I didn't check this method personally, but maybe this will suit you more than my "procedure-view" solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...