create table test_tb_grade
( id number(10) not null,
user_name varchar2(20 char),
course varchar2(20 char),
score float
);insert into test_tb_grade values(1,'michael','
語文',78);
insert into test_tb_grade values(2,'michael','數學',95);
insert into test_tb_grade values(3,'michael','英語',81);
insert into test_tb_grade values(4,'xiaoxiao','語文',97);
insert into test_tb_grade values(5,'xiaoxiao','數學',78);
insert into test_tb_grade values(6,'xiaoxiao','英語',91);
insert into test_tb_grade values(7,'zhangsan','語文',80);
insert into test_tb_grade values(8,'zhangsan','數學',55);
insert into test_tb_grade values(9,'zhangsan','英語',75);
insert into test_tb_grade values(10,'lisi','語文',87);
insert into test_tb_grade values(11,'lisi','數學',65);
insert into test_tb_grade values(12,'lisi','英語',75);
commit;
初始資料如下圖:
這就是最常見的行轉列,主要原理是利用decode函式、聚集函式(sum),結合group by分組實現的,具體的sql如下
select t.user_name,
sum(decode(t.course, '語文', score,null)) as chinese,
sum(decode(t.course, '數學', score,null)) as math,
sum(decode(t.course, '英語', score,null)) as english
from test_tb_grade t
group by t.user_name
order by t.user_name;
如果要實現對各門功課的不同分數段進行統計,效果圖如下:
具體的實現sql
如下:sql**
:select t2.score_gp,
sum(decode(t2.course, '語文', countnum,null)) as chinese,
sum(decode(t2.course, '數學', countnum,null)) as math,
sum(decode(t2.course, '英語', countnum,null)) as english
from (
select t.course,
case when t.score <60 then '00-60'
when t.score >=60 and t.score <80 then '60-80'
when t.score >=80 then '80-100' end as score_gp,
count(t.score) as countnum
from test_tb_grade t
group by t.course,
case when t.score <60 then '00-60'
when t.score >=60 and t.score <80 then '60-80'
when t.score >=80 then '80-100' end
order by t.course ) t2
group by t2.score_gp
order by t2.score_gp;
表結構: test_tb_grade2
這就是最常見的列轉行,主要原理是利用sql
裡面的union
,具體的
sql語句如下:
sql**
: select user_name, 'cn_score' course, cn_score as score from test_tb_grade2
union
select user_name, 'math_score' course, math_score as score from test_tb_grade2
union
select user_name, 'en_score' course, en_score as score from test_tb_grade2
order by user_name, course;
也可以利用
【insert all into ... select】
來實現create table laowang(id number);
create table shuangshuang(id number);
create table xiaoming(id number);
create table dashi(id number);
insert all into ... select
insert all
into laowang
into shuangshuang
into xiaoming
into dashi
select object_id from all_objects;
,首先需要先建乙個表
test_tb_grade3
:sql**:
create table test_tb_grade3
( user_name varchar2(20 char),
course varchar2(20 char),
score float
);
再執行下面的sql:
sql**:
insert all
into test_tb_grade3(user_name,course,score) values(user_name, '語文', cn_score)
into test_tb_grade3(user_name,course,score) values(user_name, '數學', math_score)
into test_tb_grade3(user_name,course,score) values(user_name, '英語', en_score)
select user_name, cn_score, math_score, en_score from test_tb_grade2;
commit;
來自為知筆記(wiz)
行轉列 列轉行
行轉列 select t.t.rowid from test1 t id c1 c2 c3 1 小紅 數學 10 2 小紅 語文 20 3 小欄 數學 15 4 小欄 語文 25 test1 oracle select c1,to char wm concat c2 c2 from test1 gr...
hive 列轉行 HQL 行轉列,列轉行
1 相關函式 concat string a col,string b col 返回輸入字串連線後的結果,支援任意個輸入字串 concat ws separator,str1,str2,它是乙個特殊形式的 concat 第乙個引數剩餘引數間的分隔符。分隔符可以是與剩餘引數一樣的字串。如果分隔符是 n...
python 列轉行 SQL 行轉列,列轉行
sql 行轉列,列轉行 行列轉換在做報表分析時還是經常會遇到的,今天就說一下如何實現行列轉換吧。行列轉換就是如下圖所示兩種展示形式的互相轉換 行轉列假如我們有下表 select from student pivot sum score for subject in 語文,數學,英語 通過上面 sql...