表 A
欄位 name mobileNo identityNo createTime updateTime
表B
欄位 name type No createTime updateTime
問題: 把表A中的資料弄到表B中,Type為二種型別(mobileNo identityNo) No 為(mobileNo identityNo)對應的值
就是把A表中的資料根據mobileNo identityNo 分成兩條插入B表
uj5u.com熱心網友回復:
樓主百度一下 unpivot , 正好適合你現在的場景uj5u.com熱心網友回復:
簡單看了一下,應該適合我。我先試試。謝謝uj5u.com熱心網友回復:
好像這個只能用于當前表對嗎?我想轉換完之后插入新表
uj5u.com熱心網友回復:
insert into t(.......)
select ....
from xxxx
unpivot ......
uj5u.com熱心網友回復:
樓主的問題其實是行轉列的問題換一個思維:行轉列
即然Type為二種型別(mobileNo identityNo)
那么你可以用case 進行判斷 或是用decode實作
----創建測驗表
create table student_score(
name varchar2(20),
subject varchar2(20),
score number(4,1)
);
-----插入測驗資料
insert into student_score (name,subject,score)values('張三','語文',78);
insert into student_score (name,subject,score)values('張三','數學',88);
insert into student_score (name,subject,score)values('張三','英語',98);
insert into student_score (name,subject,score)values('李四','語文',89);
insert into student_score (name,subject,score)values('李四','數學',76);
insert into student_score (name,subject,score)values('李四','英語',90);
insert into student_score (name,subject,score)values('王五','語文',99);
insert into student_score (name,subject,score)values('王五','數學',66);
insert into student_score (name,subject,score)values('王五','英語',91);
-----decode行轉列
select name "姓名",
sum(decode(subject, '語文', nvl(score, 0), 0)) "語文",
sum(decode(subject, '數學', nvl(score, 0), 0)) "數學",
sum(decode(subject, '英語', nvl(score, 0), 0)) "英語"
from student_score
group by name;
------ case when 行轉列
select name "姓名",
sum(case when subject='語文'
then nvl(score,0)
else 0
end) "語文",
sum(case when subject='數學'
then nvl(score,0)
else 0
end) "數學",
sum(case when subject='英語'
then nvl(score,0)
else 0
end) "英語"
from student_score
group by name;
轉換完成后加入新表無非就是在這基礎上多加一個
insert into table的陳述句
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/100766.html
標籤:開發
上一篇:誰有SQL Navigator的正式注冊碼?還有安裝包 誰有SQL Navigator的正式注冊碼?還有安裝包 誰有SQL Navigator的正式注冊碼
