表修改

时间:2016-11-11 12:34:38

标签: sql transformation

如果我有这样的表:

name attribute
john hat
john glasses
john jacket
Lisa hat
Lisa boots

我可以用什么查询来获取:

name attribute1 attribute2 attribute3
John hat         glasses   jacket
Lisa hat         boots     NULL

2 个答案:

答案 0 :(得分:0)

create local temporary table tmp_nr(NName varchar(50), maxnumber int) on commit preserve rows;
insert into tmp_nr
select Nname, count(*) as number
from T
group by Nname;

用于存储属性数量的临时表

select Nname,attributte,attributte2,attributte3 from
(
select Nname,
attributte,
lag(attributte,1) over(partition by Nname order by Nname) as attributte2,
lag(attributte,2) over(partition by Nname order by Nname) as attributte3,
row_number() over(partition by Nname order by Nname) as number
from T
)x inner join tmp_nr on x.Nname=tmp_nr.Nname
where x.number = tmp_nr.maxnumber

联接的目的是仅选择包含每个人所有项目的行。

答案 1 :(得分:-2)

ALTER TABLE table_name ADD column_name数据类型

按照此约定,通过此语法添加尽可能多的列

例如:

ALTER TABLE供应商   ADD supplier_name VARCHAR(50);