我正在使用Oracle 10g,我想将一行转换为一列。
我有这样的数据:
ID | HOMEADD | HOMETEL | WORKADD | WORKTEL
------------------------------------------
1 | HOME1 | 1234 | WORK1 | 4321
我怎样才能得到这样的结果:
ID | TYPE | ADDRESS | TEL
---------------------------
1 | HOME | HOME1 | 1234
1 | WORK | WORK1 | 4321
有没有办法使用10g进行UNPIVOT?
答案 0 :(得分:1)
您可以使用“UNION”轻松实现。
create table table1 (id number, homeadd varchar2(500), hometel varchar2(50), workadd varchar2(500), worktel varchar2(50));
insert into table1 values (1,'HOME1',1234,'WORK1',4321);
select id, 'home' as type, homeadd as address, hometel as tel from table1
union all
select id, 'work' , workadd, worktel from table1;
完整脚本:
mysql> CREATE TRIGGER ins_transaction BEFORE INSERT ON account
-> FOR EACH ROW PRECEDES ins_sum
-> SET
-> @deposits = @deposits + IF(NEW.amount>0,NEW.amount,0),
-> @withdrawals = @withdrawals + IF(NEW.amount<0,-NEW.amount,0);