创建一个选择以“去标准化”'表中的数据

时间:2017-08-01 15:07:36

标签: sql netezza

我把工作去标准化引号,因为它可能不是正确的方式,但不太确定如何描述它......

我有下表

Source   Priority   Attribute
A        1          Name
B        2          Name
C        3          Name
A        1          Address
B        2          Address
C        3          Address
A        2          Email
B        3          Email
C        1          Email

我希望我的选择返回:

Source Name_Pri  Addr_Pri  Email_Pri
A      1         1         2
B      2         2         3
C      3         3         1     

由于

1 个答案:

答案 0 :(得分:2)

您正在寻找一个支点。我经常使用条件聚合来做到这一点:

select source,
       max(case when attribute = 'Name' then priority end) as name_priority,
       max(case when attribute = 'Address' then priority end) as address_priority,
       max(case when attribute = 'Email' then priority end) as email_priority
from t
group by source;