oracle中行轉列的sql語句動態

2021-08-31 02:00:31 字數 3785 閱讀 8280

create table t_test

(city varchar2(255) not null,

year number(4) not null,

month number(2) not null,

sell_amount number(26,2)

)comment on table t_test

is '各月世聯在主要核心城市的銷售金額';

comment on column t_test.city

is '城市';

comment on column t_test.sell_amount

is '比如0.42代表42%';

insert into t_test (city, year, month, sell_amount)

values ('深圳', 2010, 5, 1.2);

insert into t_test (city, year, month, sell_amount)

values ('東莞', 2010, 5, .8);

insert into t_test (city, year, month, sell_amount)

values ('佛山', 2010, 5, .9);

insert into t_test (city, year, month, sell_amount)

values ('汕頭', 2010, 5, .22);

insert into t_test (city, year, month, sell_amount)

values ('長沙', 2010, 5, .28);

insert into t_test (city, year, month, sell_amount)

values ('深圳', 2010, 4, 1.1);

insert into t_test (city, year, month, sell_amount)

values ('東莞', 2010, 4, .8);

insert into t_test (city, year, month, sell_amount)

values ('佛山', 2010, 4, .9);

insert into t_test (city, year, month, sell_amount)

values ('廣州', 2010, 4, .67);

insert into t_test (city, year, month, sell_amount)

values ('汕頭', 2010, 4, .22);

insert into t_test (city, year, month, sell_amount)

values ('長沙', 2010, 4, .28);

insert into t_test (city, year, month, sell_amount)

values ('深圳', 2010, 3, .9);

insert into t_test (city, year, month, sell_amount)

values ('佛山', 2010, 3, .9);

insert into t_test (city, year, month, sell_amount)

values ('廣州', 2010, 3, .67);

insert into t_test (city, year, month, sell_amount)

values ('汕頭', 2010, 3, .22);

insert into t_test (city, year, month, sell_amount)

values ('長沙', 2010, 3, .28);

commit;

---儲存過程生成行轉列sql語句

create or replace procedure p_test1 is

cursor csr is

select distinct (city) from t_test;

tmp_km varchar2(400);

str varchar2(4000);

begin

/*select t.year||t.month,

sum(decode(t.city, '深圳',t.sell_amount,0)) 深圳 ,

sum(decode(t.city, '佛山',t.sell_amount,0)) 佛山,

sum(decode(t.city, '東莞',t.sell_amount,0)) 東莞

from t_test t

group by t.month,t.year

*/str := 'select t.year||t.month ';

open csr;

loop fetch csr into tmp_km;

exit when csr%notfound;

str := str || ',sum(decode(t.city, ' || chr(39) || tmp_km || chr(39) ||

',t.sell_amount,0)) ' || tmp_km;

end loop;

str := str || ' from t_test t group by t.month,t.year';

dbms_output.put_line(str);

execute immediate str;

end p_test1;

//生成集合

create or replace procedure p_test1(p_year number,p_cursor out sys_refcursor) is

cursor csr is

select distinct (city) from t_test;

tmp_km varchar2(400);

str varchar2(4000);

begin

/*select t.year||t.month,

sum(decode(t.city, '深圳',t.sell_amount,0)) 深圳 ,

sum(decode(t.city, '佛山',t.sell_amount,0)) 佛山,

sum(decode(t.city, '東莞',t.sell_amount,0)) 東莞

from t_test t

group by t.month,t.year

*/str := 'select t.year||t.month ';

open csr;

loop fetch csr into tmp_km;

exit when csr%notfound;

str := str || ',sum(decode(t.city, ' || chr(39) || tmp_km || chr(39) ||

',t.sell_amount,0)) ' || tmp_km;

end loop;

str := str || ' from t_test t where t.year='||p_year||' group by t.month,t.year';

dbms_output.put_line(str);

open p_cursor for str;

exception

when others then

dbms_output.put_line('error');

close p_cursor;

end p_test1;

sql中 行轉列 一

近一段時間一直沒怎麼看過sql了,突襲一下 行轉列,列轉行是我們在開發過程中經常碰到的問題。行轉列一般通過case when 語句來實現,也可以通過 sql server 2005 新增的運算子pivot來實現。用傳統的方法,比較好理解。層次清晰,而且比較習慣。但是pivot unpivot提供的語...

SQL中行轉列 列轉行

sql行轉列 列轉行 這個主題還是比較常見的,行轉列主要適用於對資料作聚合統計,如統計某類目的商品在某個時間區間的銷售情況。整理測試資料 create table wyc test id int 32 not null auto increment name varchar 80 default n...

SQL2005中行轉列

sql2005中有函式可以直接將行轉列,這樣行轉列就好做多了。但是數值列和文字列稍有不同。1 文字列示例 create table tb 姓名 varchar 10 課程 varchar 10 分數 int insert into tb values 張三 語文 74 insert into tb ...