create table ass(acptnum varchar(30),qu_name varchar(4000),awname varchar(2000));
insert into ass values('15101232097','問題1','答案1');
insert into ass values('15101232097','問題2','答案2');
insert into ass values('15101232097','問題3','答案3');
insert into ass values('18290890601','x','x');
insert into ass values('18290890601','x1','x2');
insert into ass values('18290890601','x2','x2');
insert into ass values('18290890601','x3','x3');
select * from ass;
acptnum qu_name awname
1 15101232097 問題1 答案1
2 15101232097 問題2 答案2
3 15101232097 問題3 答案3
4 18290890601 x x
5 18290890601 x1 x2
6 18290890601 x2 x2
7 18290890601 x3 x3
declare
c1 sys_refcursor;
v_sql varchar(1000):='';
c2 ass.qu_name%type;
begin
v_sql:='select acptnum';
open c1 for select distinct qu_name from ass;
loop
fetch c1 into c2;
exit when c1%notfound;
v_sql :=v_sql||',max(case qu_name when '''||c2||''' then awname else null end ) as ' ||c2;
end loop;
close c1;
v_sql :=v_sql|| ' from ass group by acptnum';
dbms_output.put_line(v_sql);
end;
#執行以上**生成一下sql,然後放在儲存過程裡面執行這個sql。然後輸出
select acptnum,
max(case qu_name when '問題1' then awname else null end ) as 問題1,
max(case qu_name when '問題2' then awname else null end ) as 問題2,
max(case qu_name when '問題3' then awname else null end ) as 問題3,
max(case qu_name when 'x1' then awname else null end ) as x1,
max(case qu_name when 'x3' then awname else null end ) as x3,
max(case qu_name when 'x2' then awname else null end ) as x2,
max(case qu_name when 'x' then awname else null end ) as x
from ass group by acptnum
#以下**生成sql
create or replace procedure exeute_select(v_out out sys_refcursor)
asc1 sys_refcursor;
v_sql varchar(1000):='';
c2 ass.qu_name%type;
begin
v_sql:='select acptnum';
open c1 for select distinct qu_name from ass;
loop
fetch c1 into c2;
exit when c1%notfound;
v_sql :=v_sql||',max(case qu_name when '''||c2||''' then awname else null end ) as ' ||c2;
end loop;
close c1;
v_sql :=v_sql|| ' from ass group by acptnum';
open v_out for v_sql;
end;
執行結果,將問題列的值轉換為列名了。 oracle列轉換為行
首先介紹行轉換為列,oracle行轉換為列是比較常見,網上常見的例子如下 grades表 student subject grade student1 語文 80 student1 數學 70 student1 英語 60 student2 語文 90 student2 數學 80 student2...
oracle列轉換為行
首先介紹行轉換為列,oracle行轉換為列是比較常見,網上常見的例子如下 grades表 student subject grade student1 語文 80 student1 數學 70 student1 英語 60 student2 語文 90 student2 數學 80 student2...
把某一列值轉換為逗號分隔字串
問題是這樣的。從訂閱表中select某乙個使用者所有訂閱的目錄,然後可以繫結至前台顯示給使用者,讓使用者知道自己有哪些訂閱和哪些目錄沒有訂閱。如下表 需要select使用者id為15的所有訂閱目錄。如最終結果 insus.net是在sql server 2012結合xml的for xml path ...